util.java
来自「数据仓库展示程序」· Java 代码 · 共 1,344 行 · 第 1/4 页
JAVA
1,344 行
}
public String put(String key, String value) {
for (int i = 0, n = list.size(); i < n; i++) {
String[] pair = (String[]) list.get(i);
if (pair[0].equalsIgnoreCase(key)) {
String old = pair[1];
if (key.equalsIgnoreCase("Provider")) {
// Unlike all other properties, later values of
// "Provider" do not supersede
} else {
pair[1] = value;
}
return old;
}
}
list.add(new String[] {key, value});
return null;
}
public String toString() {
StringBuffer sb = new StringBuffer(64);
for (int i = 0, n = list.size(); i < n; i++) {
String[] pair = (String[]) list.get(i);
if (i > 0) {
sb.append("; ");
}
sb.append(pair[0]);
sb.append('=');
sb.append(pair[1]);
}
return sb.toString();
}
public Iterator iterator() {
return list.iterator();
}
}
/**
* Converts an OLE DB connect string into a {@link PropertyList}.
*
* <p> For example, <code>"Provider=MSOLAP; DataSource=LOCALHOST;"</code>
* becomes the set of (key, value) pairs <code>{("Provider","MSOLAP"),
* ("DataSource", "LOCALHOST")}</code>. Another example is
* <code>Provider='sqloledb';Data Source='MySqlServer';Initial
* Catalog='Pubs';Integrated Security='SSPI';</code>.
*
* <p> This method implements as much as possible of the <a
* href="http://msdn.microsoft.com/library/en-us/oledb/htm/oledbconnectionstringsyntax.asp"
* target="_blank">OLE DB connect string syntax
* specification</a>. To find what it <em>actually</em> does, take
* a look at the <code>mondrian.olap.UtilTestCase</code> test case.
**/
public static PropertyList parseConnectString(String s) {
return new ConnectStringParser(s).parse();
}
private static class ConnectStringParser {
private final String s;
private final int n;
private int i;
private final StringBuffer nameBuf;
private final StringBuffer valueBuf;
private ConnectStringParser(String s) {
this.s = s;
this.i = 0;
this.n = s.length();
this.nameBuf = new StringBuffer(64);
this.valueBuf = new StringBuffer(64);
}
PropertyList parse() {
PropertyList list = new PropertyList();
while (i < n) {
parsePair(list);
}
return list;
}
/**
* Reads "name=value;" or "name=value<EOF>".
*/
void parsePair(PropertyList list) {
String name = parseName();
String value;
if (i >= n) {
value = "";
} else if (s.charAt(i) == ';') {
i++;
value = "";
} else {
value = parseValue();
}
list.put(name, value);
}
/**
* Reads "name=". Name can contain equals sign if equals sign is
* doubled.
*/
String parseName() {
nameBuf.setLength(0);
while (true) {
char c = s.charAt(i);
switch (c) {
case '=':
i++;
if (i < n && (c = s.charAt(i)) == '=') {
// doubled equals sign; take one of them, and carry on
i++;
nameBuf.append(c);
break;
}
String name = nameBuf.toString();
name = name.trim();
return name;
case ' ':
if (nameBuf.length() == 0) {
// ignore preceding spaces
i++;
break;
} else {
// fall through
}
default:
nameBuf.append(c);
i++;
if (i >= n) {
return nameBuf.toString().trim();
}
}
}
}
/**
* Reads "value;" or "value<EOF>"
*/
String parseValue() {
char c;
// skip over leading white space
while ((c = s.charAt(i)) == ' ') {
i++;
if (i >= n) {
return "";
}
}
if (c == '"' || c == '\'') {
String value = parseQuoted(c);
// skip over trailing white space
while (i < n && (c = s.charAt(i)) == ' ') {
i++;
}
if (i >= n) {
return value;
} else if (s.charAt(i) == ';') {
i++;
return value;
} else {
throw new RuntimeException(
"quoted value ended too soon, at position " + i +
" in '" + s + "'");
}
} else {
String value;
int semi = s.indexOf(';', i);
if (semi >= 0) {
value = s.substring(i, semi);
i = semi + 1;
} else {
value = s.substring(i);
i = n;
}
return value.trim();
}
}
/**
* Reads a string quoted by a given character. Occurrences of the
* quoting character must be doubled. For example,
* <code>parseQuoted('"')</code> reads <code>"a ""new"" string"</code>
* and returns <code>a "new" string</code>.
*/
String parseQuoted(char q) {
char c = s.charAt(i++);
Util.assertTrue(c == q);
valueBuf.setLength(0);
while (i < n) {
c = s.charAt(i);
if (c == q) {
i++;
if (i < n) {
c = s.charAt(i);
if (c == q) {
valueBuf.append(c);
i++;
continue;
}
}
return valueBuf.toString();
} else {
valueBuf.append(c);
i++;
}
}
throw new RuntimeException(
"Connect string '" + s +
"' contains unterminated quoted value '" +
valueBuf.toString() + "'");
}
}
/**
* Converts a list, which may or may not be mutable, into a mutable list.
* Non-mutable lists are returned by, for example,
* {@link List#subList}, {@link Arrays#asList},
* {@link Collections#unmodifiableList}.
*/
public static List makeMutable(List list) {
return (list instanceof ArrayList)
? list
: new ArrayList(list);
}
/**
* Combines two integers into a hash code.
*/
public static int hash(int i, int j) {
return (i << 4) ^ j;
}
/**
* Computes a hash code from an existing hash code and an object (which
* may be null).
*/
public static int hash(int h, Object o) {
int k = (o == null) ? 0 : o.hashCode();
return ((h << 4) | h) ^ k;
}
/**
* Computes a hash code from an existing hash code and an array of objects
* (which may be null).
*/
public static int hashArray(int h, Object [] a) {
// The hashcode for a null array and an empty array should be different
// than h, so use magic numbers.
if (a == null) {
return hash(h, 19690429);
}
if (a.length == 0) {
return hash(h, 19690721);
}
for (int i = 0; i < a.length; i++) {
h = hash(h, a[i]);
}
return h;
}
/**
* Returns the cumulative amount of time spent accessing the database.
*/
public static long dbTimeMillis()
{
return databaseMillis;
}
/**
* Adds to the cumulative amount of time spent accessing the database.
*/
public static void addDatabaseTime(long millis)
{
databaseMillis += millis;
}
/**
* Returns the system time less the time spent accessing the database.
* Use this method to figure out how long an operation took: call this
* method before an operation and after an operation, and the difference
* is the amount of non-database time spent.
*/
public static long nonDbTimeMillis()
{
final long systemMillis = System.currentTimeMillis();
return systemMillis - databaseMillis;
}
/**
* Creates a very simple implementation of {@link Validator}. (Only
* useful for resolving trivial expressions.)
*/
public static Validator createSimpleValidator(final FunTable funTable) {
return new Validator() {
public Query getQuery() {
throw new UnsupportedOperationException();
}
public Exp validate(Exp exp, boolean scalar) {
return exp;
}
public Parameter validate(Parameter parameter) {
return parameter;
}
public void validate(MemberProperty memberProperty) {
}
public void validate(QueryAxis axis) {
}
public void validate(Formula formula) {
}
public Exp convert(Exp fromExp, int to) {
return FunUtil.convert(fromExp, to, this);
}
public boolean canConvert(Exp fromExp, int to, int[] conversionCount) {
return true;
}
public boolean requiresExpression() {
return false;
}
public FunTable getFunTable() {
return funTable;
}
public Parameter createOrLookupParam(FunCall call) {
return null;
}
};
}
}
// End Util.java
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?