TurtleDB
A mini distributed database system
src/ca/uqac/dim/turtledb/Attribute.java
Go to the documentation of this file.
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 public class Attribute extends Literal
00021 {
00022   protected String m_value;
00023   protected String m_tableName;
00024   
00025   public Attribute()
00026   {
00027     m_value = "";
00028     m_tableName = "";
00029   }
00030   
00037   public Attribute(String s)
00038   {
00039     this();
00040     if (!s.contains("."))
00041       m_value = s;
00042     else
00043     {
00044       String[] parts = s.split("\\.");
00045       m_tableName = parts[0];
00046       m_value = parts[1];
00047     }
00048   }
00049   
00050   public Attribute(String t, String s)
00051   {
00052     this();
00053     m_tableName = t;
00054     m_value = s;
00055   }
00056   
00061   public Attribute(Attribute a)
00062   {
00063     this();
00064     if (a == null)
00065       return;
00066     m_value = new String(a.m_value);
00067     m_tableName = new String(a.m_tableName);
00068   }
00069   
00070   public void setName(String name)
00071   {
00072     m_value = name;
00073   }
00074   
00075   public void setTableName(String name)
00076   {
00077     m_tableName = name;
00078   }
00079   
00080   public String getName()
00081   {
00082     return m_value;
00083   }
00084   
00085   public String getTableName()
00086   {
00087     return m_tableName;
00088   }
00089   
00090   @Override
00091   public String toString()
00092   {
00093     String out = m_value;
00094     if (!m_tableName.isEmpty())
00095       out = m_tableName + "." + out;
00096     return out;
00097   }
00098   
00099   @Override
00100   public boolean equals(Object o)
00101   {
00102     if (o == null)
00103       return false;
00104     if (!(o instanceof Attribute))
00105       return false;
00106     return equals((Attribute) o);
00107   }
00108   
00109   public boolean equals(Attribute a)
00110   {
00111     if (a == null)
00112       return false;
00113     return m_tableName.compareTo(a.m_tableName) == 0 
00114         && m_value.compareTo(a.m_value) == 0;
00115   }
00116   
00117   @Override
00118   public int hashCode()
00119   {
00120     return m_tableName.hashCode() + m_value.hashCode();
00121   }
00122 
00123   @Override
00124   public int compareTo(Literal o)
00125   {
00126     if (o instanceof Value)
00127       return -1; // All attributes go before all values
00128     assert o instanceof Attribute;
00129     return compareTo((Attribute) o);
00130   }
00131   
00132   public int compareTo(Attribute o)
00133   {
00134     int compare_table = m_tableName.compareTo(o.m_tableName);
00135     if (compare_table < 0)
00136       return -1;
00137     if (compare_table > 0)
00138       return 1;
00139     if (compare_table == 0)
00140     {
00141       int compare_attName = m_value.compareTo(o.m_value);
00142       if (compare_attName == 0)
00143         return 0;
00144       if (compare_attName < 0)
00145         return -1;
00146       if (compare_attName > 0)
00147         return 1;
00148     }
00149     return 0;
00150   }
00151 }