Skip to content

Commit

Permalink
Add Spacer, efficient concatenation of strings of spaces. (Previous
Browse files Browse the repository at this point in the history
technique, using String.substring to create strings with a shared
backing array, does not work in JDK 1.7.0_u7 or later due to fix of
Java bug 6924259, "Remove String.count/String.offset/String.hashcode",
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6924259.)

Use Spacer in ParseTreeWriter.

Tidy up javadoc.


git-svn-id: https://olap4j.svn.sourceforge.net/svnroot/olap4j/trunk@553 c6a108a4-781c-0410-a6c6-c2d559e19af0
  • Loading branch information
julianhyde committed Nov 27, 2012
1 parent 432c4d1 commit 39bd69b
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 99 deletions.
50 changes: 13 additions & 37 deletions src/org/olap4j/OlapConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ public interface OlapConnection extends Connection, OlapWrapper {
* first one available.
*
* @return The name of the database currently active for this connection.
* @throws OlapException
* An exception will be thrown, if any of these conditions
* are true:
* @throws OlapException if any of these conditions are true:
* <ul>
* <li>A server error occurs.</li>
* <li>No databases exist on the server.</li>
Expand All @@ -109,11 +107,8 @@ public interface OlapConnection extends Connection, OlapWrapper {
* Sets the name of the database that will be used for this connection.
* Overrides the value passed, if any, through the JDBC URL.
*
* @param databaseName
* The name of the database to use.
* @throws OlapException
* An exception will be thrown, if any of these conditions
* are true:
* @param databaseName The name of the database to use.
* @throws OlapException if any of these conditions are true:
* <ul>
* <li>A server error occurs.</li>
* <li>The user specified a database name which does not
Expand All @@ -133,9 +128,7 @@ public interface OlapConnection extends Connection, OlapWrapper {
* @see #getOlapDatabases()
* @return The currently active Database, or null of none are currently
* selected.
* @throws OlapException
* An exception will be thrown, if any of these conditions
* are true:
* @throws OlapException if any of these conditions are true:
* <ul>
* <li>A server error occurs.</li>
* <li>No databases exist on the server.</li>
Expand Down Expand Up @@ -168,9 +161,7 @@ public interface OlapConnection extends Connection, OlapWrapper {
* the driver will also use the first one available on the server.
*
* @return The name of the catalog which is active for this connection.
* @throws OlapException
* An exception will be thrown, if any of these conditions
* are true:
* @throws OlapException if any of these conditions are true:
* <ul>
* <li>A server error occurs.</li>
* <li>No database name was specified and no databases exist
Expand All @@ -189,11 +180,8 @@ public interface OlapConnection extends Connection, OlapWrapper {
* Sets the name of the catalog that will be used for this connection.
* Overrides the value passed, if any, through the JDBC URL.
*
* @param catalogName
* The name of the catalog to use for this connection.
* @throws OlapException
* An exception will be thrown, if any of these conditions
* are true:
* @param catalogName The name of the catalog to use for this connection.
* @throws OlapException if any of these conditions are true:
* <ul>
* <li>A server error occurs.</li>
* <li>No database name was specified and no databases
Expand All @@ -219,9 +207,7 @@ public interface OlapConnection extends Connection, OlapWrapper {
*
* @return The currently active catalog, or null of none are
* currently selected.
* @throws OlapException
* An exception will be thrown, if any of these conditions
* are true:
* @throws OlapException if any of these conditions are true:
* <ul>
* <li>A server error occurs.</li>
* <li>No database name was specified and no databases
Expand Down Expand Up @@ -250,9 +236,7 @@ public interface OlapConnection extends Connection, OlapWrapper {
* if the caller modifies the list, behavior is undefined.
*
* @return List of Catalogs in this connection's OLAP server
* @throws OlapException
* An exception will be thrown, if any of these conditions
* are true:
* @throws OlapException if any of these conditions are true:
* <ul>
* <li>A server error occurs.</li>
* <li>No database name was specified and no databases
Expand All @@ -276,9 +260,7 @@ public interface OlapConnection extends Connection, OlapWrapper {
* account the specified Database, Catalog and Schema names, if any.
*
* @return The name of the schema currently selected for this connection.
* @throws OlapException
* An exception will be thrown, if any of these conditions
* are true:
* @throws OlapException if any of these conditions are true:
* <ul>
* <li>A server error occurs.</li>
* <li>No database name was specified and no databases
Expand All @@ -302,9 +284,7 @@ public interface OlapConnection extends Connection, OlapWrapper {
* Overrides the value passed, if any, through the JDBC URL.
*
* @param schemaName The name of the schema to use for this connection.
* @throws OlapException
* An exception will be thrown, if any of these conditions
* are true:
* @throws OlapException if any of these conditions are true:
* <ul>
* <li>A server error occurs.</li>
* <li>No database name was specified and no databases
Expand Down Expand Up @@ -335,9 +315,7 @@ public interface OlapConnection extends Connection, OlapWrapper {
* account the specified Database, Catalog and Schema names, if any.
*
* @return The currently active schema
* @throws OlapException
* An exception will be thrown, if any of these conditions
* are true:
* @throws OlapException if any of these conditions are true:
* <ul>
* <li>A server error occurs.</li>
* <li>No database name was specified and no databases
Expand Down Expand Up @@ -371,9 +349,7 @@ public interface OlapConnection extends Connection, OlapWrapper {
* if the caller modifies the list, behavior is undefined.
*
* @return List of Catalogs in this connection's OLAP server
* @throws OlapException
* An exception will be thrown, if any of these conditions
* are true:
* @throws OlapException if any of these conditions are true:
* <ul>
* <li>A server error occurs.</li>
* <li>No database name was specified and no databases
Expand Down
33 changes: 6 additions & 27 deletions src/org/olap4j/mdx/ParseTreeWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
package org.olap4j.mdx;

import org.olap4j.impl.Spacer;

import java.io.PrintWriter;
import java.io.Writer;

Expand Down Expand Up @@ -49,11 +51,9 @@
*/
public class ParseTreeWriter {
private final PrintWriter pw;
private int linePrefixLength;
private String linePrefix;
private final Spacer spacer = new Spacer();

private static final int INDENT = 4;
private static String bigString = " ";

/**
* Creates a ParseTreeWriter.
Expand All @@ -74,11 +74,9 @@ public ParseTreeWriter(Writer w) {
@Override
public void println() {
super.println();
print(linePrefix);
spacer.spaces(this);
}
};
this.linePrefixLength = 0;
setPrefix();
}

/**
Expand All @@ -94,33 +92,14 @@ public PrintWriter getPrintWriter() {
* Increases the indentation level.
*/
public void indent() {
linePrefixLength += INDENT;
setPrefix();
}

private void setPrefix() {
linePrefix = spaces(linePrefixLength);
spacer.add(INDENT);
}

/**
* Decreases the indentation level.
*/
public void outdent() {
linePrefixLength -= INDENT;
setPrefix();
}

/**
* Returns a string of N spaces.
* @param n Number of spaces
* @return String of N spaces
*/
private static synchronized String spaces(int n)
{
while (n > bigString.length()) {
bigString = bigString + bigString;
}
return bigString.substring(0, n);
spacer.subtract(INDENT);
}
}

Expand Down
20 changes: 6 additions & 14 deletions src/org/olap4j/transform/DrillDownOnPositionTransform.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
/**
* Drill down on position transform
*
* TODO: transform to be completed, not working for now.
* <p>TODO: transform to be completed, not working for now.</p>
*
* <p>Description: Adds the children of a member at a specific position on an
* axis. The member to drill is identified from a CellSet with the axis,
Expand All @@ -51,21 +51,17 @@
*/
public class DrillDownOnPositionTransform extends AxisTransform {

// private final int positionOrdinalInAxis;
// private final int memberOrdinalInPosition;
// private final CellSet cellSet;

private final Position positionToDrill;
private final Member memberToDrill;
private final List<Member> pathToMember;

/**
* ctor
* Creates a DrillDownOnPositionTransform.
*
* @param axis
* @param positionOrdinalInAxis
* @param memberOrdinalInPosition
* @param cellSet
* @param axis Axis
* @param positionOrdinalInAxis Position ordinal on axis
* @param memberOrdinalInPosition Member ordinal in Position
* @param cellSet Cell set
*/
public DrillDownOnPositionTransform(
Axis axis,
Expand All @@ -74,10 +70,6 @@ public DrillDownOnPositionTransform(
CellSet cellSet)
{
super(axis);
// this.positionOrdinalInAxis = positionOrdinalInAxis;
// this.memberOrdinalInPosition = memberOrdinalInPosition;
// this.cellSet = cellSet;

positionToDrill =
TransformUtil.getPositionFromCellSet(
axis, positionOrdinalInAxis, cellSet);
Expand Down
26 changes: 5 additions & 21 deletions src/org/olap4j/transform/RollUpLevelTransform.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,15 @@
*/
public class RollUpLevelTransform extends AxisTransform {

// private final int positionOrdinalInAxis;
// private final int memberOrdinalInPosition;
// private final CellSet cellSet;

// private final Position positionToDrill;
private final Member memberToDrill;
// private final List<Member> pathToMember;

/**
* ctor
* Creates a RollUpLevelTransform.
*
* @param axis
* @param positionOrdinalInAxis
* @param memberOrdinalInPosition
* @param cellSet
* @param axis Axis
* @param positionOrdinalInAxis Position ordinal on Axis
* @param memberOrdinalInPosition Member ordinal in Position
* @param cellSet Cell set
*/
public RollUpLevelTransform(
Axis axis,
Expand All @@ -69,17 +63,8 @@ public RollUpLevelTransform(
{
super(axis);

// this.positionOrdinalInAxis = positionOrdinalInAxis;
// this.memberOrdinalInPosition = memberOrdinalInPosition;
// this.cellSet = cellSet;

// Position positionToDrill =
// TransformUtil.getPositionFromCellSet(axis, positionOrdinalInAxis,
// cellSet);
memberToDrill = TransformUtil.getMemberFromCellSet(
axis, positionOrdinalInAxis, memberOrdinalInPosition, cellSet);
// pathToMember = getPathToMember(positionToDrill,
// memberOrdinalInPosition);
}

public String getName() {
Expand All @@ -101,7 +86,6 @@ protected ParseTreeNode processAxisExp(ParseTreeNode exp) {
MdxHelper.makeParentCallNode(
MdxHelper.makeMemberNode(memberToDrill)))));
}

}

// End RollUpLevelTransform.java
37 changes: 37 additions & 0 deletions testsrc/org/olap4j/impl/Olap4jUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import junit.framework.TestCase;

import java.io.*;
import java.util.*;

/**
Expand Down Expand Up @@ -685,6 +686,7 @@ public void testLocaleToLcid() {

// Locale en_SG was previously unknown, now known.
String s = Integer.toHexString(18441);
assertEquals("4809", s);
assertEquals(0x4809, LcidLocale.localeToLcid(new Locale("en", "SG")));

// Locale en_XX is unknown; fall back to en.
Expand Down Expand Up @@ -722,6 +724,41 @@ public void testParseLocale() {
}
}

/**
* Unit test for {@link org.olap4j.impl.Spacer}.
*/
public void testSpacer() {
final Spacer spacer = new Spacer();
assertEquals("", spacer.toString());
spacer.add(2);
assertEquals(2, spacer.toString().length());

final StringWriter stringWriter = new StringWriter();
spacer.spaces(stringWriter);
assertEquals(2, stringWriter.toString().length());

try {
spacer.spaces((Writer) stringWriter);
} catch (IOException e) {
throw new RuntimeException("fail", e);
}
assertEquals(4, stringWriter.toString().length());

final PrintWriter printWriter = new PrintWriter(stringWriter);
spacer.spaces(printWriter);
printWriter.flush();
assertEquals(6, stringWriter.toString().length());

final StringBuilder stringBuilder = new StringBuilder();
spacer.spaces(stringBuilder);
assertEquals(2, stringBuilder.toString().length());

spacer.add(3);
assertEquals(5, spacer.toString().length());

spacer.subtract(1);
assertEquals(4, spacer.toString().length());
}
}

// End Olap4jUtilTest.java

0 comments on commit 39bd69b

Please sign in to comment.