Skip to content

Commit

Permalink
Add methods .elementName and .asMap() to NamedList.
Browse files Browse the repository at this point in the history
git-svn-id: https://olap4j.svn.sourceforge.net/svnroot/olap4j/trunk@509 c6a108a4-781c-0410-a6c6-c2d559e19af0
  • Loading branch information
julianhyde committed Feb 24, 2012
1 parent 75f5545 commit 5e11a1f
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 25 deletions.
9 changes: 9 additions & 0 deletions src/org/olap4j/driver/xmla/DeferredNamedListImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.olap4j.metadata.NamedList;

import java.util.AbstractList;
import java.util.Map;

/**
* Named list which instantiates itself on first use.
Expand Down Expand Up @@ -116,6 +117,14 @@ public int indexOfName(String name) {
return getList().indexOfName(name);
}

public String elementName(Object element) {
return getList().elementName(element);
}

public Map<String, T> asMap() {
return getList().asMap();
}

protected void populateList(NamedList<T> list) throws OlapException {
context.olap4jConnection.populateList(
list, context, metadataRequest, handler, restrictions);
Expand Down
9 changes: 5 additions & 4 deletions src/org/olap4j/driver/xmla/XmlaOlap4jCellSetMetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,16 @@ class XmlaOlap4jCellSetMetaData implements CellSetMetaData {
final XmlaOlap4jCube cube;
private final NamedList<CellSetAxisMetaData> axisMetaDataList =
new ArrayNamedListImpl<CellSetAxisMetaData>() {
protected String getName(CellSetAxisMetaData axisMetaData) {
return axisMetaData.getAxisOrdinal().name();
public String elementName(Object axisMetaData) {
return ((CellSetAxisMetaData) axisMetaData).getAxisOrdinal()
.name();
}
};
private final XmlaOlap4jCellSetAxisMetaData filterAxisMetaData;
private final NamedList<Property> cellProperties =
new ArrayNamedListImpl<Property>() {
protected String getName(Property property) {
return property.getName();
public String elementName(Object property) {
return ((Property) property).getName();
}
};
final Map<String, Property> propertiesByTag;
Expand Down
4 changes: 2 additions & 2 deletions src/org/olap4j/driver/xmla/XmlaOlap4jLevel.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ public Type getLevelType() {

public NamedList<Property> getProperties() {
final NamedList<Property> list = new ArrayNamedListImpl<Property>() {
protected String getName(Property property) {
return property.getName();
public String elementName(Object property) {
return ((Property) property).getName();
}
};
// standard properties first
Expand Down
14 changes: 8 additions & 6 deletions src/org/olap4j/impl/AbstractNamedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@

import org.olap4j.metadata.NamedList;

import java.util.AbstractList;
import java.util.*;

/**
* Partial implementation of {@link org.olap4j.metadata.NamedList}.
*
* <p>Derived class must implement {@link #get(int)} and {@link #size()}, as
* per {@link java.util.AbstractList}; and must implement
* {@link #getName(Object)}, to indicate how elements are named.
* {@link #elementName(Object)}, to indicate how elements are named.
*
* @see org.olap4j.impl.ArrayNamedListImpl
*
Expand All @@ -40,11 +40,9 @@ public abstract class AbstractNamedList<T>
extends AbstractList<T>
implements NamedList<T>
{
protected abstract String getName(T t);

public T get(String name) {
for (T t : this) {
if (getName(t).equals(name)) {
if (elementName(t).equals(name)) {
return t;
}
}
Expand All @@ -54,12 +52,16 @@ public T get(String name) {
public int indexOfName(String name) {
for (int i = 0; i < size(); ++i) {
T t = get(i);
if (getName(t).equals(name)) {
if (elementName(t).equals(name)) {
return i;
}
}
return -1;
}

public Map<String, T> asMap() {
return new NamedListMap<T>(this);
}
}

// End AbstractNamedList.java
15 changes: 8 additions & 7 deletions src/org/olap4j/impl/ArrayNamedListImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@

import org.olap4j.metadata.NamedList;

import java.util.ArrayList;
import java.util.Collection;
import java.util.*;

/**
* Implementation of {@link org.olap4j.metadata.NamedList} which uses
* {@link java.util.ArrayList} for storage.
*
* <p>Derived class must implement {@link #getName(Object)}, to indicate how
* <p>Derived class must implement {@link #elementName(Object)}, to indicate how
* elements are named.
*
* @see NamedListImpl
Expand Down Expand Up @@ -71,11 +70,9 @@ public ArrayNamedListImpl(Collection<? extends T> c) {
super(c);
}

protected abstract String getName(T t);

public T get(String name) {
for (T t : this) {
if (getName(t).equals(name)) {
if (elementName(t).equals(name)) {
return t;
}
}
Expand All @@ -85,12 +82,16 @@ public T get(String name) {
public int indexOfName(String name) {
for (int i = 0; i < size(); ++i) {
T t = get(i);
if (getName(t).equals(name)) {
if (elementName(t).equals(name)) {
return i;
}
}
return -1;
}

public Map<String, T> asMap() {
return new NamedListMap<T>(this);
}
}

// End ArrayNamedListImpl.java
4 changes: 2 additions & 2 deletions src/org/olap4j/impl/NamedListImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public NamedListImpl(Collection<? extends T> c) {
super(c);
}

protected final String getName(T t) {
return t.getName();
public final String elementName(Object t) {
return ((T) t).getName();
}
}

Expand Down
72 changes: 72 additions & 0 deletions src/org/olap4j/impl/NamedListMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
// $Id: ArrayNamedListImpl.java 482 2012-01-05 23:27:27Z jhyde $
//
// Licensed to Julian Hyde under one or more contributor license
// agreements. See the NOTICE file distributed with this work for
// additional information regarding copyright ownership.
//
// Julian Hyde licenses this file to you 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.olap4j.impl;

import org.olap4j.metadata.NamedList;

import java.util.*;

/**
* Map backed by a {@link org.olap4j.metadata.NamedList}.
*
* @author jhyde
* @version $Id: AbstractNamedList.java 482 2012-01-05 23:27:27Z jhyde $
*/
class NamedListMap<T> extends AbstractMap<String, T> {
private final NamedList<T> namedList;

/**
* Creates a NamedListMap.
*
* @param namedList Named list
*/
public NamedListMap(NamedList<T> namedList) {
this.namedList = namedList;
}

public Set<Entry<String, T>> entrySet() {
return new AbstractSet<Entry<String, T>>() {
public Iterator<Entry<String, T>> iterator() {
final Iterator<T> iterator = namedList.iterator();
return new Iterator<Entry<String, T>>() {
public boolean hasNext() {
return iterator.hasNext();
}

public Entry<String, T> next() {
T x = iterator.next();
String name = namedList.elementName(x);
return new Pair<String, T>(name, x);
}

public void remove() {
iterator.remove();
}
};
}

public int size() {
return namedList.size();
}
};
}
}

// End NamedListMap.java
16 changes: 12 additions & 4 deletions src/org/olap4j/impl/Olap4jUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,8 @@ public static <T> NamedList<T> unmodifiableNamedList(
final NamedList<? extends T> list)
{
return list instanceof RandomAccess
? new UnmodifiableNamedRandomAccessList<T>(list)
: new UnmodifiableNamedList<T>(list);
? new UnmodifiableNamedRandomAccessList<T>(list)
: new UnmodifiableNamedList<T>(list);
}

/**
Expand Down Expand Up @@ -615,10 +615,10 @@ private enum DummyEnum {
}

/**
* Implementation of {@link NamedList} whih is immutable and empty.
* Implementation of {@link NamedList} that is immutable and empty.
*/
private static class EmptyNamedList<T> extends AbstractNamedList<T> {
protected String getName(Object o) {
public String elementName(Object element) {
throw new UnsupportedOperationException();
}

Expand Down Expand Up @@ -652,6 +652,14 @@ public int indexOfName(String s) {
return list.indexOfName(s);
}

public String elementName(Object element) {
return list.elementName(element);
}

public Map<String, T> asMap() {
return Collections.unmodifiableMap(list.asMap());
}

public int size() {
return list.size();
}
Expand Down
17 changes: 17 additions & 0 deletions src/org/olap4j/metadata/NamedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.olap4j.metadata;

import java.util.List;
import java.util.Map;

/**
* Extension to {@link java.util.List} which allows access to members of the
Expand Down Expand Up @@ -54,6 +55,22 @@ public interface NamedList<E> extends List<E> {
* @see #indexOf(Object)
*/
int indexOfName(String name);

/**
* Returns the name of a given element.
*
* @param element Element
* @return Name of element
*/
String elementName(Object element);

/**
* Returns a view of this named list as a {@link Map} whose key is the name
* of each element.
*
* @return A view of this named list as a map
*/
Map<String, E> asMap();
}

// End NamedList.java

0 comments on commit 5e11a1f

Please sign in to comment.