KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > lib > BasicTupleStructure


1 /**
2  * MEDOR: Middleware Enabling Distributed Object Requests
3  *
4  * Copyright (C) 2001-2003 France Telecom R&D
5  * Contact: alexandre.lefebvre@rd.francetelecom.com
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * Initial developers: M. Alia, A. Lefebvre
22  */

23 package org.objectweb.medor.lib;
24
25 import org.objectweb.medor.api.TupleStructure;
26 import org.objectweb.medor.api.Field;
27 import org.objectweb.medor.api.MedorException;
28 import org.objectweb.medor.clone.api.Cloneable;
29 import org.objectweb.medor.clone.lib.BasicCloneable;
30
31 import org.objectweb.util.monolog.api.Logger;
32
33 import java.util.HashMap JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.Map JavaDoc;
36
37 public class BasicTupleStructure extends BasicCloneable implements TupleStructure {
38
39     protected ArrayList JavaDoc fields = new ArrayList JavaDoc();
40     protected HashMap JavaDoc name2field = new HashMap JavaDoc();
41     transient protected Logger logger;
42
43     protected BasicTupleStructure() {
44         logger = Log.getLoggerFactory().getLogger(getClass().getName());
45     }
46
47     public BasicTupleStructure(Field[] fields) throws MedorException {
48         this();
49         for (int cpt = 0; cpt < fields.length; cpt++) {
50             // strores the fields indexed by rank
51
this.fields.add(fields[cpt]);
52             // strores the fields indexed by name
53
if (name2field.put(fields[cpt].getName(), fields[cpt]) != null) {
54                 throw new MedorException("Duplicated Field Name: " + fields[cpt].getName());
55             }
56         }
57     }
58
59     public Object JavaDoc clone(Object JavaDoc clone,
60                         Map JavaDoc obj2clone) throws CloneNotSupportedException JavaDoc {
61         clone = super.clone(clone, obj2clone);
62         BasicTupleStructure bts = (BasicTupleStructure) clone;
63         bts.fields = new ArrayList JavaDoc(fields.size());
64         bts.name2field = new HashMap JavaDoc();
65         for(int i=0; i<fields.size(); i++) {
66             Field f = (Field) getClone((Cloneable JavaDoc) fields.get(i), obj2clone);
67             bts.fields.add(f);
68             bts.name2field.put(f.getName(), f);
69         }
70         return clone;
71     }
72
73     public Field[] getFields() {
74         return (Field[]) fields.toArray(new Field[fields.size()]);
75     }
76
77     public Field getField(String JavaDoc fieldname) throws MedorException {
78         Field res = (Field) name2field.get(fieldname);
79         if (res==null) {
80             throw new MedorException("Field name error: " + fieldname);
81         }
82         return res;
83     }
84
85     public Field getField(int fieldrank) throws MedorException {
86         Field f = (Field) fields.get(fieldrank - 1);
87         if (f == null) {
88             throw new MedorException("Field rank error : " + fieldrank);
89         } else {
90             return f;
91         }
92     }
93
94     public int getFieldRank(Field f) throws MedorException {
95         int idx = fields.indexOf(f);
96         if (idx == -1)
97             throw new MedorException("Field not found: " + f.getName());
98         else return idx+1;
99     }
100
101
102     public int getSize() {
103         return fields.size();
104     }
105
106     public boolean contains(Field f) {
107         return fields.contains(f);
108     }
109
110     public boolean contains(String JavaDoc fieldName) {
111         return name2field.containsKey(fieldName);
112     }
113 }
114
Popular Tags