Skip to content

Commit

Permalink
MONDRIAN: Replace Member.ArrayEquals with ArrayHolder (which does the…
Browse files Browse the repository at this point in the history
… same thing and is more general-purpose).

    Review comments.

[git-p4: depot-paths = "//open/mondrian/": change = 8655]
  • Loading branch information
julianhyde committed Feb 5, 2007
1 parent e2bbdc5 commit 5a6a86f
Show file tree
Hide file tree
Showing 12 changed files with 1,079 additions and 1,200 deletions.
35 changes: 0 additions & 35 deletions src/main/mondrian/olap/Member.java
Expand Up @@ -13,7 +13,6 @@

package mondrian.olap;

import java.util.Arrays;
/**
* A <code>Member</code> is a 'point' on a dimension of a cube. Examples are
* <code>[Time].[1997].[January]</code>,
Expand All @@ -35,40 +34,6 @@
* defines which are allowed.
*/
public interface Member extends OlapElement, Comparable {

/**
* ArrayEquals allows two arrays of members (Member[]) that are
* different arrays but have the same Members in their arrays
* to be used as the same key to a Map. What is required is an
* equality test over all members of the array since one can not use
* the naked array of members as a key.
*/
public class ArrayEquals {

private final Member[] members;

// For use when you know the Object is a Member[].
public ArrayEquals(Object o) {
this((Member[]) o);
}
public ArrayEquals(Member[] members) {
this.members = members;
}
public Member[] get() {
return this.members;
}
public boolean equals(Object o) {
if (o instanceof ArrayEquals) {
ArrayEquals other = (ArrayEquals) o;
return Arrays.equals(this.members, other.members);
} else {
return false;
}
}
public int hashCode() {
return Arrays.hashCode(this.members);
}
}

/**
* Returns this member's parent, or null (not the 'null member', as
Expand Down
67 changes: 15 additions & 52 deletions src/main/mondrian/olap/fun/FunUtil.java
Expand Up @@ -51,7 +51,7 @@ public class FunUtil extends Util {
* has returned the MDX EMPTY value. See {@link DoubleCalc}.
*/
public static final double DoubleEmpty = -0.000000012345;

/**
* Special value which indicates that an <code>int</code> computation
* has returned the MDX null value. See {@link mondrian.calc.IntegerCalc}.
Expand Down Expand Up @@ -91,11 +91,11 @@ public static final boolean isMemberType(Calc calc) {

public static final void checkIterListResultStyles(Calc calc) {
switch (calc.getResultStyle()) {
case ITERABLE :
case LIST :
case MUTABLE_LIST :
case ITERABLE:
case LIST:
case MUTABLE_LIST:
break;
default :
default:
throw ResultStyleException.generateBadType(
new ResultStyle[] {
ResultStyle.ITERABLE,
Expand All @@ -105,12 +105,13 @@ public static final void checkIterListResultStyles(Calc calc) {
calc.getResultStyle());
}
}

public static final void checkListResultStyles(Calc calc) {
switch (calc.getResultStyle()) {
case LIST :
case MUTABLE_LIST :
case LIST:
case MUTABLE_LIST:
break;
default :
default:
throw ResultStyleException.generateBadType(
new ResultStyle[] {
ResultStyle.LIST,
Expand Down Expand Up @@ -386,7 +387,7 @@ static Map<Object, Object> evaluateTuples(
if (result == null) {
result = Util.nullValue;
}
mapMemberToValue.put(new Member.ArrayEquals(tuples), result);
mapMemberToValue.put(new ArrayHolder<Member>(tuples), result);
}
return mapMemberToValue;
}
Expand Down Expand Up @@ -641,16 +642,16 @@ static void toPercent(List members, Map mapMemberToValue, boolean isMember) {
Object o = (isMember)
? mapMemberToValue.get(members.get(i))
: mapMemberToValue.get(
new Member.ArrayEquals(members.get(i)));
new ArrayHolder<Member>((Member []) members.get(i)));
if (o instanceof Number) {
total += ((Number) o).doubleValue();
}
}
for (int i = 0; i < memberCount; i++) {
Object mo = members.get(i);
Object o = (isMember)
? mapMemberToValue.get(mo)
: mapMemberToValue.get(new Member.ArrayEquals(mo));
Object o = (isMember) ?
mapMemberToValue.get(mo) :
mapMemberToValue.get(new ArrayHolder<Member>((Member []) mo));
if (o instanceof Number) {
double d = ((Number) o).doubleValue();
if (isMember) {
Expand All @@ -659,7 +660,7 @@ static void toPercent(List members, Map mapMemberToValue, boolean isMember) {
d / total * (double) 100);
} else {
mapMemberToValue.put(
new Member.ArrayEquals(mo),
new ArrayHolder<Member>((Member []) mo),
d / total * (double) 100);
}
}
Expand Down Expand Up @@ -2143,44 +2144,6 @@ public int compare(Object o1, Object o2) {
}
}

/**
* Holds an array, so that {@link #equals} and {@link #hashCode} work.
*/
protected static class ArrayHolder {
private Object[] a;

ArrayHolder(Object[] a) {
this.a = a;
}

public int hashCode() {
int h = 0;
for (int i = 0; i < a.length; i++) {
Object o = a[i];
int rotated = (h << 4) | ((h >> 28) & 0xf);
h = rotated ^ o.hashCode();
}
return h;
}

public boolean equals(Object o) {
return o instanceof ArrayHolder &&
equals(a, ((ArrayHolder) o).a);
}

private static boolean equals(Object[] a1, Object[] a2) {
if (a1.length != a2.length) {
return false;
}
for (int i = 0; i < a1.length; i++) {
if (!a1[i].equals(a2[i])) {
return false;
}
}
return true;
}
}

static class SetWrapper {
List v = new ArrayList();
public int errorCount = 0, nullCount = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/main/mondrian/olap/fun/GenerateFunDef.java
Expand Up @@ -158,12 +158,12 @@ private void addDistinct(
List<Object> result,
List<Object> result2,
Set<Object> emitted) {

for (Object row : result2) {
Object entry = row;
if (entry instanceof Member []) {
// wrap array for correct distinctness test
entry = new Member.ArrayEquals(entry);
entry = new ArrayHolder<Member>((Member []) entry);
}
if (emitted.add(entry)) {
result.add(row);
Expand Down
4 changes: 2 additions & 2 deletions src/main/mondrian/olap/fun/IntersectFunDef.java
Expand Up @@ -35,7 +35,7 @@ class IntersectFunDef extends FunDefBase
new String[] {"fxxxy", "fxxx"},
IntersectFunDef.class,
ReservedWords);

public IntersectFunDef(FunDef dummyFunDef)
{
super(dummyFunDef);
Expand Down Expand Up @@ -86,7 +86,7 @@ private static Collection buildSearchableCollection(Collection right) {
Object element = iter.next();

if (element instanceof Object[]) {
element = new FunUtil.ArrayHolder((Object[])element);
element = new ArrayHolder((Object[])element);
}

result.add(element);
Expand Down
2 changes: 1 addition & 1 deletion src/main/mondrian/olap/fun/TopBottomPercentSumFunDef.java
Expand Up @@ -141,7 +141,7 @@ public List evaluateList(Evaluator evaluator) {
Object o = (isMember)
? mapMemberToValue.get(list.get(i))
: mapMemberToValue.get(
new Member.ArrayEquals(list.get(i)));
new ArrayHolder<Member>((Member []) list.get(i)));
if (o == Util.nullValue) {
nullCount++;
} else if (o instanceof Number) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/mondrian/olap/fun/UnionFunDef.java
Expand Up @@ -61,7 +61,7 @@ public List evaluateList(Evaluator evaluator) {
};
}

List union(List list0, List list1, final boolean all) {
<T> List<T> union(List<T> list0, List<T> list1, final boolean all) {
assert list0 != null;
assert list1 != null;
if (all) {
Expand All @@ -71,13 +71,13 @@ List union(List list0, List list1, final boolean all) {
if (list1.isEmpty()) {
return list0;
}
List result = new ArrayList();
List<T> result = new ArrayList<T>();
result.addAll(list0);
result.addAll(list1);
return result;
} else {
Set added = new HashSet();
List result = new ArrayList();
List<T> result = new ArrayList<T>();
FunUtil.addUnique(result, list0, added);
FunUtil.addUnique(result, list1, added);
return result;
Expand Down

0 comments on commit 5a6a86f

Please sign in to comment.