Source code: jacomma/icm/io/imp/EncoderTemplate.java
1 /*
2 * $Source: /home/data/cvsroot/src/jacomma/icm/io/imp/EncoderTemplate.java,v $
3 * $Revision: 1.9 $
4 * $Date: 2000/10/28 20:09:07 $
5 *
6 * This file is part of the jacomma framework
7 * Copyright (c) 2000 Dimitrios Vyzovitis
8 * mailto:dviz@egnatia.ee.auth.gr
9 *
10 *
11 *
12 *
13 *
14 *
15 * This library is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU Library General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU Library General Public License for more details.
24 *
25 * You should have received a copy of the GNU Library General Public License
26 * along with this library; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330,
28 * Boston, MA 02111-1307 USA
29 */
30
31 package jacomma.icm.io.imp;
32
33 import jacomma.icm.io.EncObject;
34 import jacomma.icm.io.EncObjectType;
35 import jacomma.icm.io.EncObjectSignature;
36 import jacomma.icm.io.ObjectEncoder;
37
38 import jacomma.util.Cache;
39
40 import java.util.Iterator;
41 import java.util.Set;
42
43 /**
44 * TBA
45 **/
46 public abstract class EncoderTemplate implements ObjectEncoder {
47
48 protected Set all_ = new java.util.HashSet( 4 );
49 protected Set some_ = new java.util.HashSet( 4 );
50 protected boolean sign_ = false; // default behavior for signatures
51 protected Cache cache_ = new Cache();
52
53 protected EncoderTemplate(){}
54
55 public EncObject encode( Object obj ) {
56 return encode( obj, null, sign_ );
57 }
58
59 public EncObject encode( Object obj, boolean sign ) {
60 return encode( obj, null, sign );
61 }
62
63 public EncObject encode( Object obj, Object hint ) {
64 return encode( obj, hint, sign_ );
65 }
66
67 protected EncObject sign( String s, EncObject obj ) {
68 CacheKey key = new CacheKey( obj, s, true );
69 EncObject sobj = (EncObject)cache_.get( key );
70 if ( sobj == null ) {
71 obj.setSignature( s );
72 sobj = (EncObject)cache_.try_put( key, Registry.instance.getEncoder( obj ).encode( obj ) );
73 }
74
75 return sobj;
76 }
77
78 protected EncObject sign( EncObject obj ) {
79 return sign( EncObjectSignature.signatureOf( obj ), obj );
80 }
81
82 public boolean canHandle( Object obj ) {
83 return canHandle( obj.getClass() );
84 }
85
86 public boolean canHandleSome( Class c ) {
87 return check( c, some_ );
88 }
89
90 public boolean canHandle( Class c ) {
91 return check( c, all_ );
92 }
93
94
95 protected boolean check( Class c, Set set ) {
96 boolean r;
97 if ( !(r = set.contains( c )) )
98 // ok, let's check whether it is a sub-class
99 for ( Iterator it = set.iterator();
100 !r && it.hasNext();
101 r = ((Class)it.next()).isInstance( c ) );
102
103 return r;
104 }
105
106
107 // utility class for creating cache keys for Objects to be encoded
108 // Note that signed objects are stored using a different key to avoid
109 // confusion (that key is the object itself).
110 protected class CacheKey {
111 Object obj_;
112 Object hint_;
113 boolean sig_;
114
115 public CacheKey( Object obj, Object hint, boolean sig ) {
116 obj_ = obj;
117 hint_ = hint;
118 sig_ = sig;
119 }
120
121 public CacheKey( Object obj, boolean sig ) {
122 obj_ = obj;
123 sig_ = sig;
124 }
125
126 public CacheKey( Object obj, Object hint ) {
127 obj_ = obj;
128 hint_ = hint;
129 }
130
131 public CacheKey( Object obj ) {
132 obj_ = obj;
133 }
134
135 public int hashCode() {
136 return obj_.hashCode();
137 }
138
139 public boolean equals( Object obj ) {
140 try {
141 return obj_.equals( ((CacheKey)obj).obj_ )
142 && ( (hint_ == null) ?
143 (((CacheKey)obj).hint_ == null) :
144 hint_.equals( ((CacheKey)obj).hint_ ) )
145 && sig_ == ((CacheKey)obj).sig_;
146 } catch ( ClassCastException exc ) {
147 return false;
148 }
149 }
150
151 }
152 }