PageRenderTime 20ms CodeModel.GetById 15ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/src/solidstack/query/ValuesMap.java

http://solidstack.googlecode.com/
Java | 118 lines | 74 code | 18 blank | 26 comment | 5 complexity | 8a5d9bc5ff4380967d9d652a2a1169ce MD5 | raw file
Possible License(s): Apache-2.0
  1/*--
  2 * Copyright 2010 René M. de Bloois
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License");
  5 * you may not use this file except in compliance with the License.
  6 * You may obtain a copy of the License at
  7 *
  8 *     http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS,
 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 * See the License for the specific language governing permissions and
 14 * limitations under the License.
 15 */
 16
 17package solidstack.query;
 18
 19import java.io.Serializable;
 20import java.util.Collection;
 21import java.util.Map;
 22import java.util.Set;
 23
 24import solidstack.util.ObjectArrayList;
 25
 26/**
 27 * Decorates an {@link Object} array to let it look like a {@link Map}.
 28 * 
 29 * @author René M. de Bloois
 30 */
 31public class ValuesMap implements Map< String, Object >, Serializable
 32{
 33	private static final long serialVersionUID = 1L;
 34
 35	private Map< String, Integer > names; // This one is shared by all instances
 36	private Object[] values;
 37
 38	/**
 39	 * Constructor.
 40	 * 
 41	 * @param names The names and indexes of the elements in the {@link Object} array.
 42	 * @param values The values.
 43	 */
 44	public ValuesMap( Map< String, Integer > names, Object[] values )
 45	{
 46		this.names = names;
 47		this.values = values;
 48	}
 49
 50	public int size()
 51	{
 52		return this.values.length;
 53	}
 54
 55	public boolean isEmpty()
 56	{
 57		return this.values.length == 0;
 58	}
 59
 60	public boolean containsKey( Object key )
 61	{
 62		if( !( key instanceof String ) )
 63			throw new IllegalArgumentException( "Expecting a string" );
 64		String k = ( (String)key ).toLowerCase();
 65		return this.names.containsKey( k );
 66	}
 67
 68	public Object get( Object key )
 69	{
 70		if( !( key instanceof String ) )
 71			throw new IllegalArgumentException( "Expecting a string" );
 72		String k = ( (String)key ).toLowerCase();
 73		Integer index = this.names.get( k );
 74		if( index == null )
 75			throw new IllegalArgumentException( "Unknown column name: " + key );
 76		return this.values[ index ];
 77	}
 78
 79	public Set< String > keySet()
 80	{
 81		return this.names.keySet();
 82	}
 83
 84	public Collection< Object > values()
 85	{
 86		return new ObjectArrayList( this.values );
 87	}
 88
 89	public boolean containsValue( Object value )
 90	{
 91		throw new UnsupportedOperationException();
 92	}
 93
 94	public Object put( String key, Object value )
 95	{
 96		throw new UnsupportedOperationException();
 97	}
 98
 99	public Object remove( Object key )
100	{
101		throw new UnsupportedOperationException();
102	}
103
104	public void putAll( Map< ? extends String, ? extends Object > m )
105	{
106		throw new UnsupportedOperationException();
107	}
108
109	public void clear()
110	{
111		throw new UnsupportedOperationException();
112	}
113
114	public Set< java.util.Map.Entry< String, Object >> entrySet()
115	{
116		throw new UnsupportedOperationException();
117	}
118}