/src/solidstack/query/ValuesMap.java

http://solidstack.googlecode.com/ · Java · 118 lines · 74 code · 18 blank · 26 comment · 5 complexity · 8a5d9bc5ff4380967d9d652a2a1169ce MD5 · raw file

  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. package solidstack.query;
  17. import java.io.Serializable;
  18. import java.util.Collection;
  19. import java.util.Map;
  20. import java.util.Set;
  21. import solidstack.util.ObjectArrayList;
  22. /**
  23. * Decorates an {@link Object} array to let it look like a {@link Map}.
  24. *
  25. * @author René M. de Bloois
  26. */
  27. public class ValuesMap implements Map< String, Object >, Serializable
  28. {
  29. private static final long serialVersionUID = 1L;
  30. private Map< String, Integer > names; // This one is shared by all instances
  31. private Object[] values;
  32. /**
  33. * Constructor.
  34. *
  35. * @param names The names and indexes of the elements in the {@link Object} array.
  36. * @param values The values.
  37. */
  38. public ValuesMap( Map< String, Integer > names, Object[] values )
  39. {
  40. this.names = names;
  41. this.values = values;
  42. }
  43. public int size()
  44. {
  45. return this.values.length;
  46. }
  47. public boolean isEmpty()
  48. {
  49. return this.values.length == 0;
  50. }
  51. public boolean containsKey( Object key )
  52. {
  53. if( !( key instanceof String ) )
  54. throw new IllegalArgumentException( "Expecting a string" );
  55. String k = ( (String)key ).toLowerCase();
  56. return this.names.containsKey( k );
  57. }
  58. public Object get( Object key )
  59. {
  60. if( !( key instanceof String ) )
  61. throw new IllegalArgumentException( "Expecting a string" );
  62. String k = ( (String)key ).toLowerCase();
  63. Integer index = this.names.get( k );
  64. if( index == null )
  65. throw new IllegalArgumentException( "Unknown column name: " + key );
  66. return this.values[ index ];
  67. }
  68. public Set< String > keySet()
  69. {
  70. return this.names.keySet();
  71. }
  72. public Collection< Object > values()
  73. {
  74. return new ObjectArrayList( this.values );
  75. }
  76. public boolean containsValue( Object value )
  77. {
  78. throw new UnsupportedOperationException();
  79. }
  80. public Object put( String key, Object value )
  81. {
  82. throw new UnsupportedOperationException();
  83. }
  84. public Object remove( Object key )
  85. {
  86. throw new UnsupportedOperationException();
  87. }
  88. public void putAll( Map< ? extends String, ? extends Object > m )
  89. {
  90. throw new UnsupportedOperationException();
  91. }
  92. public void clear()
  93. {
  94. throw new UnsupportedOperationException();
  95. }
  96. public Set< java.util.Map.Entry< String, Object >> entrySet()
  97. {
  98. throw new UnsupportedOperationException();
  99. }
  100. }