TurtleDB
A mini distributed database system
|
00001 /*------------------------------------------------------------------------- 00002 Simple distributed database engine 00003 Copyright (C) 2012 Sylvain Hallé 00004 00005 This program is free software: you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation, either version 3 of the License, or 00008 (at your option) any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with this program. If not, see <http://www.gnu.org/licenses/>. 00017 -------------------------------------------------------------------------*/ 00018 package ca.uqac.dim.turtledb; 00019 00020 import java.util.*; 00021 00027 public class Tuple implements Comparable<Tuple> 00028 { 00029 protected Vector<Attribute> m_attributes; 00030 protected Vector<Value> m_values; 00031 00032 /*package*/ Tuple() 00033 { 00034 super(); 00035 m_attributes = new Vector<Attribute>(); 00036 m_values = new Vector<Value>(); 00037 } 00038 00043 public Tuple(Tuple t) 00044 { 00045 this(); 00046 assert t != null && t.m_attributes != null && t.m_values != null; 00047 for (Attribute a : t.m_attributes) 00048 { 00049 m_attributes.add(a); 00050 } 00051 for (Value v : t.m_values) 00052 { 00053 m_values.add(v); 00054 } 00055 } 00056 00057 public Tuple(Schema sch, Value[] val) 00058 { 00059 this(); 00060 assert sch.size() == val.length; 00061 for (int i = 0; i < val.length; i++) 00062 { 00063 this.put(sch.elementAt(i), val[i]); 00064 } 00065 } 00066 00067 public Tuple(Schema sch, String values) 00068 { 00069 this(); 00070 String parts[] = values.split("[,\\s]"); 00071 assert sch.size() == parts.length; 00072 for (int i = 0; i < parts.length; i++) 00073 { 00074 this.put(sch.elementAt(i), new Value(parts[i])); 00075 } 00076 } 00077 00083 public void setSchema(Schema sch) 00084 { 00085 assert sch.size() == m_attributes.size(); 00086 m_attributes.clear(); 00087 for (Attribute a : sch) 00088 { 00089 m_attributes.add(a); 00090 } 00091 } 00092 00093 public Value get(Literal a) 00094 { 00095 // TODO: eventually replace by a Map search 00096 for (int i = 0; i < m_attributes.size(); i++) 00097 { 00098 Attribute att = m_attributes.elementAt(i); 00099 if (att.equals(a)) 00100 return m_values.elementAt(i); 00101 } 00102 return null; 00103 } 00104 00105 public void clear() 00106 { 00107 m_attributes.clear(); 00108 m_values.clear(); 00109 } 00110 00111 public void put(Attribute a, Value v) 00112 { 00113 m_attributes.add(a); 00114 m_values.add(v); 00115 } 00116 00117 public void putAll(Tuple t) 00118 { 00119 m_attributes.addAll(t.m_attributes); 00120 m_values.addAll(t.m_values); 00121 } 00122 00123 public Set<Attribute> keySet() 00124 { 00125 Set<Attribute> out = new HashSet<Attribute>(); 00126 out.addAll(m_attributes); 00127 return out; 00128 } 00129 00134 public void setTable(String name) 00135 { 00136 for (Attribute a : m_attributes) 00137 { 00138 a.setTableName(name); 00139 } 00140 } 00141 00146 @Override 00147 public int compareTo(Tuple t) 00148 { 00149 if (m_values.size() < t.m_values.size()) 00150 return -1; 00151 if (m_values.size() > t.m_values.size()) 00152 return 1; 00153 assert m_values.size() == t.m_values.size(); 00154 for (int i = 0; i < m_values.size(); i++) 00155 { 00156 Value v1 = m_values.get(i); 00157 Value v2 = t.m_values.get(i); 00158 int comp = v1.compareTo(v2); 00159 if (comp < 0) 00160 return -1; 00161 if (comp > 0) 00162 return 1; 00163 } 00164 return 0; 00165 } 00166 00167 public int size() 00168 { 00169 return m_values.size(); 00170 } 00171 00172 @Override 00173 public String toString() 00174 { 00175 StringBuilder out = new StringBuilder(); 00176 for (int i = 0; i < m_values.size(); i++) 00177 { 00178 if (i > 0) 00179 out.append(","); 00180 Attribute a = m_attributes.get(i); 00181 Value v = m_values.get(i); 00182 out.append(a).append("=").append(v); 00183 } 00184 return out.toString(); 00185 } 00186 00191 public int getDegree() 00192 { 00193 return m_attributes.size(); 00194 } 00195 00196 @Override 00197 public boolean equals(Object o) 00198 { 00199 if (o == null) 00200 return false; 00201 if (!(o instanceof Tuple)) 00202 return false; 00203 assert o instanceof Tuple; 00204 return equals((Tuple) o); 00205 } 00206 00207 public boolean equals(Tuple t) 00208 { 00209 if (t == null) 00210 return false; 00211 if (t.m_attributes.size() != m_attributes.size()) 00212 return false; 00213 if (t.m_values.size() != m_values.size()) 00214 return false; 00215 for (int i = 0; i < m_values.size(); i++) 00216 { 00217 if (!m_attributes.get(i).equals(t.m_attributes.get(i))) 00218 return false; 00219 if (!m_values.get(i).equals(t.m_values.get(i))) 00220 return false; 00221 } 00222 return true; 00223 } 00224 00230 public static Tuple makeTuple(Vector<Tuple> v) 00231 { 00232 Tuple t = new Tuple(); 00233 for (Tuple tt : v) 00234 { 00235 if (tt == null) 00236 return null; 00237 t.putAll(tt); 00238 } 00239 return t; 00240 } 00241 }