KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > spi > orb > ParserImplTableBase


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