krysalis-update/src/java/org/krysalis/depot/common/util Tuple.java,NONE,1.1 SystemUtils.java,NONE,1.1 IndentWriter.java,NONE,1.1 Triple.java,NONE,1.1 Registry.java,NONE,1.1
Nick Chalko <[email protected]> Wed, 08 Dec 2004 09:06:34 +0000
| Newsgroups | gmane.comp.krysalis.cvs |
|---|---|
| Message-ID | <[email protected]> |
Update of /cvsroot/krysalis/krysalis-update/src/java/org/krysalis/depot/common/util
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24726/src/java/org/krysalis/depot/common/util
Added Files:
Tuple.java SystemUtils.java IndentWriter.java Triple.java
Registry.java
Log Message:
Copied from the apache incubator.
--- NEW FILE: Registry.java ---
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.krysalis.depot.common.util;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.TreeMap;
import org.krysalis.depot.common.DepotError;
import org.krysalis.depot.common.util.debug.DebugUtils;
import org.krysalis.depot.common.util.debug.Dumpable;
/**
* @author ajack.
*/
public class Registry implements Dumpable {
private TreeMap m_contents = null;
private boolean m_uniqueContents = false;
public Registry() {
m_uniqueContents = false;
m_contents = new TreeMap();
}
public Registry(boolean uniqueContents) {
m_uniqueContents = uniqueContents;
m_contents = new TreeMap();
}
protected void put(Object entry) {
put(entry, entry);
}
protected void put(Object key, Object entry) {
if (m_uniqueContents && m_contents.containsKey(key)) {
throw new DepotError(
"Can't have same item twice. Entry: " + key + " : " + entry);
}
m_contents.put(key, entry);
}
public Iterator iterator() {
return m_contents.values().iterator();
}
protected Object get(Object key) {
return m_contents.get(key);
}
public boolean hasEntries() {
return !m_contents.isEmpty();
}
public void dump(PrintWriter out, int depth, boolean verbose) {
final String indent = DebugUtils.getIndent(depth);
final String indent1 = DebugUtils.getIndent(depth + 1);
out.print(indent);
out.println("Registry :" + SystemUtils.getShortName(getClass()));
if (hasEntries()) {
out.print(indent1);
out.println("(");
for (Iterator i = iterator(); i.hasNext();) {
DebugUtils.dump(out,depth+2,verbose,(Object) i.next());
}
out.print(indent1);
out.println(")");
}
}
//
// public int compareTo(Object other) {
// int comparison = -1;
// String name = getClass().getName();
// String otherName = other.getClass().getName();
// comparison = name.compareTo(otherName);
// return comparison;
// }
public Object[] toArray() {
return m_contents.entrySet().toArray();
}
public int size() {
return m_contents.size();
}
}
--- NEW FILE: Tuple.java ---
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.krysalis.depot.common.util;
/**
* @author ajack
*/
public class Tuple {
private Object m_first = null;
private Object m_second = null;
public Tuple(Object first, Object second) {
m_first = first;
m_second = second;
}
/**
* Returns the first.
* @return Object
*/
public Object getFirst() {
return m_first;
}
/**
* Returns the second.
* @return Object
*/
public Object getSecond() {
return m_second;
}
/**
* Sets the first.
* @param first The first to set
*/
public void setFirst(Object first) {
m_first = first;
}
/**
* Sets the second.
* @param second The second to set
*/
public void setSecond(Object second) {
m_second = second;
}
}
--- NEW FILE: SystemUtils.java ---
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.krysalis.depot.common.util;
import java.io.File;
import java.io.PrintWriter;
/**
* @author ajack
*/
public class SystemUtils {
public static final String g_pathSeparator;
static {
// :TODO: --- BTW: Add forceLog --- to stderr if not logging..
g_pathSeparator = getSystemProperty("path.separator");
}
public static File getCWD() {
File cwd = null;
try {
cwd = new File(getSystemProperty("user.dir"));
}
catch (Exception e) {
// World of hurt...
// :TODO:
}
return cwd;
}
public static String getSystemProperty(String key) {
String value = null;
try {
value = System.getProperty(key);
}
catch (SecurityException se) {
// World of hurt...
// :TODO:
}
return value;
}
public static String getShortName(Object obj) {
return getShortName(obj.getClass().getName());
}
public static String getShortName(Class clazz) {
return getShortName(clazz.getName());
}
public static String getShortName(String name) {
String shortName = null;
int periodPos = name.lastIndexOf('.');
if (-1 != periodPos)
shortName = name.substring(periodPos + 1);
else
shortName = name;
return shortName;
}
public static PrintWriter getSystemOut() {
//:TODO: Singleton?
return new PrintWriter(System.out, true);
}
public static PrintWriter getSystemErr() {
//:TODO: Singleton?
return new PrintWriter(System.err, true);
}
public static Triple getMemoryInformation() {
Runtime r = Runtime.getRuntime();
Long total = new Long(r.totalMemory());
Long free = new Long(r.freeMemory());
Long max = new Long(r.maxMemory());
return new Triple(total,free,max);
}
}
--- NEW FILE: Triple.java ---
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.krysalis.depot.common.util;
/**
* @author ajack
*/
public class Triple extends Tuple {
private Object m_third = null;
public Triple(Object first, Object second, Object third) {
super(first,second);
m_third = third;
}
/**
* @return
*/
public Object getThird() {
return m_third;
}
/**
* @param object
*/
public void setThird(Object object) {
m_third = object;
}
}
--- NEW FILE: IndentWriter.java ---
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.krysalis.depot.common.util;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Stack;
/**
* @author chalko
*
* A PrintWriter that provides line indentations.
*
*/
public class IndentWriter extends PrintWriter {
private int m_indentLevel = 0;
private int m_defaultIndent = 4;
private String m_indentString = "";
private StringBuffer m_indentBuffer = new StringBuffer();
//TODO: make static, but thread safe
private Stack m_lastLevelStack = new Stack();
private StringBuffer m_buffer = new StringBuffer();
private boolean m_indented = false;
/**
* Constructor for IndentWriter.
* @param out
*/
public IndentWriter(Writer out) {
super(out);
}
/**
* Constructor for IndentWriter.
* @param out
* @param autoFlush
*/
public IndentWriter(Writer out, boolean autoFlush) {
super(out, autoFlush);
}
/**
* Constructor for IndentWriter.
* @param out
*/
public IndentWriter(OutputStream out) {
super(out);
}
/**
* Constructor for IndentWriter.
* @param out
* @param autoFlush
*/
public IndentWriter(OutputStream out, boolean autoFlush) {
super(out, autoFlush);
}
/**
* Returns the indentLevel.
* @return int
*/
public int getIndent() {
return m_indentLevel;
}
/**
* Sets the indentLevel.
* @param indentLevel The indentLevel to set
*/
private void setIndent(int indent) {
this.m_indentLevel = Math.max(0, indent);
while (m_indentBuffer.length() < indent) {
m_indentBuffer.append(" ");
}
m_indentString = m_indentBuffer.substring(0, indent);
// clear lastLevel greater than what we are at
while (!m_lastLevelStack.isEmpty()
&& ((Integer) m_lastLevelStack.peek()).intValue() > m_indentLevel) {
m_lastLevelStack.pop();
}
}
/**
* Increase the indent level by the defaultIndent.
* Remembers the current indent level for later undenting.
*/
public void indent() {
m_lastLevelStack.push(new Integer(m_indentLevel));
setIndent(m_indentLevel + m_defaultIndent);
}
/**
* Restores the previos indent level.
*/
public void undent() {
if (!m_lastLevelStack.isEmpty()) {
Integer i = (Integer) m_lastLevelStack.pop();
setIndent(i.intValue());
} else {
setIndent(0);
}
}
private void writeIndentIfNeeded() {
if (!m_indented) {
m_indented = true;
super.write(m_indentString);
}
}
/**
* Returns the defaultIndent.
* @return int
*/
public int getDefaultIndent() {
return m_defaultIndent;
}
/**
* Sets the defaultIndent.
* @param defaultIndent The defaultIndent to set
*/
public void setDefaultIndent(int defaultIndent) {
this.m_defaultIndent = Math.max(1, defaultIndent);
}
/**
* @see java.io.Writer#write(char[], int, int)
*/
public void write(char[] cbuf, int off, int len) {
super.write(cbuf, off, len);
writeIndentIfNeeded();
}
/**
* @see java.io.Writer#write(char[])
*/
public void write(char[] cbuf) {
super.write(cbuf);
writeIndentIfNeeded();
}
/**
* @see java.io.Writer#write(int)
*/
public void write(int c) {
super.write(c);
writeIndentIfNeeded();
}
/**
* @see java.io.Writer#write(String, int, int)
*/
public void write(String str, int off, int len) {
super.write(str, off, len);
writeIndentIfNeeded();
}
/**
* @see java.io.Writer#write(String)
*/
public void write(String str) {
super.write(str);
writeIndentIfNeeded();
}
/**
* @see java.io.PrintWriter#println(boolean)
*/
public void println(boolean x) {
writeIndentIfNeeded();
super.println(x);
m_indented = false;
}
/**
* @see java.io.PrintWriter#println(char)
*/
public void println(char x) {
writeIndentIfNeeded();
super.println(x);
m_indented = false;
}
/**
* @see java.io.PrintWriter#println(char[])
*/
public void println(char[] x) {
writeIndentIfNeeded();
super.println(x);
m_indented = false;
}
/**
* @see java.io.PrintWriter#println(double)
*/
public void println(double x) {
writeIndentIfNeeded();
super.println(x);
m_indented = false;
}
/**
* @see java.io.PrintWriter#println(float)
*/
public void println(float x) {
writeIndentIfNeeded();
super.println(x);
m_indented = false;
}
/**
* @see java.io.PrintWriter#println(int)
*/
public void println(int x) {
writeIndentIfNeeded();
super.println(x);
m_indented = false;
}
/**
* @see java.io.PrintWriter#println(long)
*/
public void println(long x) {
writeIndentIfNeeded();
super.println(x);
m_indented = false;
}
/**
* @see java.io.PrintWriter#println(Object)
*/
public void println(Object x) {
writeIndentIfNeeded();
super.println(x);
m_indented = false;
}
/**
* @see java.io.PrintWriter#println(String)
*/
public void println(String x) {
writeIndentIfNeeded();
super.println(x);
m_indented = false;
}
/**
* @see java.io.PrintWriter#println()
*/
public void println() {
writeIndentIfNeeded();
super.println();
m_indented = false;
}
/**
* @see java.io.PrintWriter#print(boolean)
*/
public void print(boolean b) {
writeIndentIfNeeded();
super.print(b);
}
/**
* @see java.io.PrintWriter#print(char)
*/
public void print(char c) {
writeIndentIfNeeded();
super.print(c);
}
/**
* @see java.io.PrintWriter#print(char[])
*/
public void print(char[] s) {
writeIndentIfNeeded();
super.print(s);
}
/**
* @see java.io.PrintWriter#print(double)
*/
public void print(double d) {
writeIndentIfNeeded();
super.print(d);
}
/**
* @see java.io.PrintWriter#print(float)
*/
public void print(float f) {
writeIndentIfNeeded();
super.print(f);
}
/**
* @see java.io.PrintWriter#print(int)
*/
public void print(int i) {
writeIndentIfNeeded();
super.print(i);
}
/**
* @see java.io.PrintWriter#print(long)
*/
public void print(long l) {
writeIndentIfNeeded();
super.print(l);
}
/**
* @see java.io.PrintWriter#print(Object)
*/
public void print(Object obj) {
writeIndentIfNeeded();
super.print(obj);
}
/**
* @see java.io.PrintWriter#print(String)
*/
public void print(String s) {
writeIndentIfNeeded();
super.print(s);
}
}
-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://productguide.itmanagersjournal.com/