1. /*
  2. * @(#)ParserImplTableBase.java 1.7 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.corba.se.spi.orb ;
  8. import java.util.Map ;
  9. import java.util.AbstractMap ;
  10. import java.util.Set ;
  11. import java.util.AbstractSet ;
  12. import java.util.Iterator ;
  13. import java.util.Properties ;
  14. import java.lang.reflect.Field ;
  15. import org.omg.CORBA.INTERNAL ;
  16. // XXX This could probably be further extended by using more reflection and
  17. // a dynamic proxy that satisfies the interfaces that are inherited by the
  18. // more derived class. Do we want to go that far?
  19. public abstract class ParserImplTableBase extends ParserImplBase {
  20. private final ParserData[] entries ;
  21. public ParserImplTableBase( ParserData[] entries )
  22. {
  23. this.entries = entries ;
  24. setDefaultValues() ;
  25. }
  26. protected PropertyParser makeParser()
  27. {
  28. PropertyParser result = new PropertyParser() ;
  29. for (int ctr=0; ctr<entries.length; ctr++ ) {
  30. ParserData entry = entries[ctr] ;
  31. entry.addToParser( result ) ;
  32. }
  33. return result ;
  34. }
  35. private static final class MapEntry implements Map.Entry {
  36. private Object key ;
  37. private Object value ;
  38. public MapEntry( Object key )
  39. {
  40. this.key = key ;
  41. }
  42. public Object getKey()
  43. {
  44. return key ;
  45. }
  46. public Object getValue()
  47. {
  48. return value ;
  49. }
  50. public Object setValue( Object value )
  51. {
  52. Object result = this.value ;
  53. this.value = value ;
  54. return result ;
  55. }
  56. public boolean equals( Object obj )
  57. {
  58. if (!(obj instanceof MapEntry))
  59. return false ;
  60. MapEntry other = (MapEntry)obj ;
  61. return (key.equals( other.key )) &&
  62. (value.equals( other.value )) ;
  63. }
  64. public int hashCode()
  65. {
  66. return key.hashCode() ^ value.hashCode() ;
  67. }
  68. }
  69. // Construct a map that maps field names to test or default values,
  70. // then use setFields from the parent class. A map is constructed
  71. // by implementing AbstractMap, which requires implementing the
  72. // entrySet() method, which requires implementing a set of
  73. // map entries, which requires implementing an iterator,
  74. // which iterates over the ParserData, extracting the
  75. // correct (key, value) pairs (nested typed lambda expression).
  76. private static class FieldMap extends AbstractMap {
  77. private final ParserData[] entries ;
  78. private final boolean useDefault ;
  79. public FieldMap( ParserData[] entries, boolean useDefault )
  80. {
  81. this.entries = entries ;
  82. this.useDefault = useDefault ;
  83. }
  84. public Set entrySet()
  85. {
  86. return new AbstractSet()
  87. {
  88. public Iterator iterator()
  89. {
  90. return new Iterator() {
  91. // index of next element to return
  92. int ctr = 0 ;
  93. public boolean hasNext()
  94. {
  95. return ctr < entries.length ;
  96. }
  97. public Object next()
  98. {
  99. ParserData pd = entries[ctr++] ;
  100. Map.Entry result = new MapEntry( pd.getFieldName() ) ;
  101. if (useDefault)
  102. result.setValue( pd.getDefaultValue() ) ;
  103. else
  104. result.setValue( pd.getTestValue() ) ;
  105. return result ;
  106. }
  107. public void remove()
  108. {
  109. throw new UnsupportedOperationException() ;
  110. }
  111. } ;
  112. }
  113. public int size()
  114. {
  115. return entries.length ;
  116. }
  117. } ;
  118. }
  119. } ;
  120. protected void setDefaultValues()
  121. {
  122. Map map = new FieldMap( entries, true ) ;
  123. setFields( map ) ;
  124. }
  125. public void setTestValues()
  126. {
  127. Map map = new FieldMap( entries, false ) ;
  128. setFields( map ) ;
  129. }
  130. }