Skip to content

Commit

Permalink
Add CellSetFormatter, a couple of implementations, and a few tests.
Browse files Browse the repository at this point in the history
git-svn-id: https://olap4j.svn.sourceforge.net/svnroot/olap4j/trunk@156 c6a108a4-781c-0410-a6c6-c2d559e19af0
  • Loading branch information
julianhyde committed Apr 19, 2009
1 parent 93d335a commit 93fc515
Show file tree
Hide file tree
Showing 6 changed files with 1,431 additions and 102 deletions.
122 changes: 122 additions & 0 deletions src/org/olap4j/impl/CoordinateIterator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
// $Id$
// This software is subject to the terms of the Common Public License
// Agreement, available at the following URL:
// http://www.opensource.org/licenses/cpl.html.
// Copyright (C) 2009-2009 Julian Hyde
// All Rights Reserved.
// You must accept the terms of that agreement to use this software.
*/
package org.olap4j.impl;

import java.util.Iterator;

/**
* Iterator over the coordinates of a hyper-rectangle.
*
* <p>For example, {@code new CoordinateIterator(new int[] {3, 2})} generates
* the pairs {@code {0, 0}, {0, 1}, {1, 0}, {1, 1}, {2, 0}, {2, 1} }.
*
* @author jhyde
* @version $Id$
* @since Apr 7, 2009
*/
public class CoordinateIterator implements Iterator<int[]> {
private final int[] dimensions;
private final boolean littleEndian;
private final int[] current;
private boolean hasNext;

/**
* Creates a big-endian coordinate iterator.
*
* @param dimensions Array containing the number of elements of each
* coordinate axis
*/
public CoordinateIterator(int[] dimensions) {
this(dimensions, false);
}

/**
* Creates a coordinate iterator.
*
* @param dimensions Array containing the number of elements of each
* @param littleEndian Whether coordinate 0 is the least significant
* (and fastest changing) coordinate
*/
public CoordinateIterator(int[] dimensions, boolean littleEndian) {
this.dimensions = dimensions;
this.littleEndian = littleEndian;
this.current = new int[dimensions.length];
this.hasNext = true;
for (int dimension : dimensions) {
if (dimension <= 0) {
// an axis is empty. no results will be produced
hasNext = false;
break;
}
}
}

/**
* Creates an {@link Iterable} that yields a
* {@link org.olap4j.impl.CoordinateIterator}.
*
* <p>Useful in a foreach loop, for example:
*
* <blockquote>int[] dimensions;
* for (int[] coords : CoordinateIterator.iterate(dimensions)) {
* foo(coords);
* }
* </blockquote>
*
* @param dimensions Array containing the number of elements of each
* coordinate axis
*
* @return Iterable
*/
public static Iterable<int[]> iterate(final int[] dimensions) {
return new Iterable<int[]>() {
public Iterator<int[]> iterator() {
return new CoordinateIterator(dimensions, true);
}
};
}

public boolean hasNext() {
return hasNext;
}

public int[] next() {
final int[] result = current.clone();
moveToNext();
return result;
}

private void moveToNext() {
if (littleEndian) {
for (int offset = 0; offset < dimensions.length; ++offset) {
int k = ++current[offset];
if (k < dimensions[offset]) {
return;
}
current[offset] = 0;
}
} else {
for (int offset = dimensions.length - 1; offset >= 0; --offset) {
int k = ++current[offset];
if (k < dimensions[offset]) {
return;
}
current[offset] = 0;
}
}
hasNext = false;
}

public void remove() {
throw new UnsupportedOperationException();
}
}

// End CoordinateIterator.java
38 changes: 38 additions & 0 deletions src/org/olap4j/query/CellSetFormatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
// $Id$
// This software is subject to the terms of the Common Public License
// Agreement, available at the following URL:
// http://www.opensource.org/licenses/cpl.html.
// Copyright (C) 2009-2009 Julian Hyde
// All Rights Reserved.
// You must accept the terms of that agreement to use this software.
*/
package org.olap4j.query;

import org.olap4j.CellSet;

import java.io.PrintWriter;

/**
* Converts a {@link CellSet} into text.
*
* <p><b>This interface is experimental. It is not part of the olap4j
* specification and is subject to change without notice.</b></p>
*
* @author jhyde
* @version $Id$
* @since Apr 15, 2009
*/
public interface CellSetFormatter {
/**
* Formats a CellSet as text to a PrintWriter.
*
* @param cellSet Cell set
* @param pw Print writer
*/
void format(
CellSet cellSet,
PrintWriter pw);
}

// End CellSetFormatter.java
Loading

0 comments on commit 93fc515

Please sign in to comment.