1 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 ; 34 import java.util.ArrayList ; 35 import java.util.Map ; 36 37 public class BasicTupleStructure extends BasicCloneable implements TupleStructure { 38 39 protected ArrayList fields = new ArrayList (); 40 protected HashMap name2field = new HashMap (); 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 this.fields.add(fields[cpt]); 52 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 clone(Object clone, 60 Map obj2clone) throws CloneNotSupportedException { 61 clone = super.clone(clone, obj2clone); 62 BasicTupleStructure bts = (BasicTupleStructure) clone; 63 bts.fields = new ArrayList (fields.size()); 64 bts.name2field = new HashMap (); 65 for(int i=0; i<fields.size(); i++) { 66 Field f = (Field) getClone((Cloneable ) 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 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 fieldName) { 111 return name2field.containsKey(fieldName); 112 } 113 } 114 | Popular Tags |