//This file is part of SECONDO. //Copyright (C) 2004, University in Hagen, Department of Computer Science, //Database Systems for New Applications. //SECONDO is free software; you can redistribute it and/or modify //it under the terms of the GNU General Public License as published by //the Free Software Foundation; either version 2 of the License, or //(at your option) any later version. //SECONDO is distributed in the hope that it will be useful, //but WITHOUT ANY WARRANTY; without even the implied warranty of //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //GNU General Public License for more details. //You should have received a copy of the GNU General Public License //along with SECONDO; if not, write to the Free Software //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA package viewer.hoese; import java.util.*; import sj.lang.ListExpr; /** this class provides a link from attribute values to category names */ public class AttrCatList{ private String Name; private TreeSet Links; private static int no = 0; // automatic names /** create a new AttrCatList with given name */ public AttrCatList(String Name){ Links = new TreeSet(new LinkComparator()); if (Name==null) this.Name ="Attribute_Category_Link_"+no++; else this.Name=Name; } /** create a new AttrCatList with automatic name */ public AttrCatList(){ this("Attribute_Category_Link_"+no++); } /** returns the name of this AttrCatList */ public String getName(){ return Name; } /** set the Name */ public void setName(String N){ if(N!=null); Name=N; } /** add a new Link * @return true, if the pair (Value,CatName) is not contained in * the set of links */ public boolean addLink(String Value,String CatName){ return Links.add(new Link(Value,CatName)); } /** Returns all contained category names **/ public Iterator getCatNames(){ TreeSet CN = new TreeSet(new StringComparator()); Iterator it = Links.iterator(); while (it.hasNext()){ CN.add(((Link)it.next()).getCatName()); } return CN.iterator(); } /** Returns all category names assigned to a value from the list. **/ public Iterator getCatNames(ListExpr le){ TreeSet cn = new TreeSet(new StringComparator()); int at = le.atomType(); if(at!=ListExpr.NO_ATOM){ // not a list String s = getCatName(le); if(s!=null){ cn.add(s); } } else{ ListExpr scan = le; while(!scan.isEmpty()){ String s = getCatName(scan.first()); if(s!=null){ cn.add(s); } scan = scan.rest(); } } return cn.iterator(); } /** add a new Link * @return true, if the pair (Value,CatName) is not contained in * the set of links */ public boolean addLink(double Value,String CatName){ return Links.add(new Link(Value,CatName)); } /** add a new Link * @return true, if the pair (Value,CatName) is not contained in * the set of links */ public boolean addLink(boolean Value,String CatName){ return Links.add(new Link(Value,CatName)); } /** add a new Link * @return true, if the pair (Value,CatName) is not contained in * the set of links */ public boolean addLink(int Value,String CatName){ return Links.add(new Link(Value,CatName)); } /** set a new category name for a given value * @return false if the link set not contains a link with given value */ public boolean updateLink(int Value,String CatName){ Link SearchLink = new Link(Value,""); if(!Links.contains(SearchLink)) return false; Link L = (Link) Links.tailSet(SearchLink).first(); L.setCatName(CatName); return true; } /** set a new category name for a given value * @return false if the link set not contains a link with given value */ public boolean updateLink(String Value,String CatName){ Link SearchLink = new Link(Value,""); if(!Links.contains(SearchLink)) return false; Link L = (Link) Links.tailSet(SearchLink).first(); L.setCatName(CatName); return true; } /** set a new category name for a given value * @return false if the link set not contains a link with given value */ public boolean updateLink(boolean Value,String CatName){ Link SearchLink = new Link(Value,""); if(!Links.contains(SearchLink)) return false; Link L = (Link) Links.tailSet(SearchLink).first(); L.setCatName(CatName); return true; } /** set a new category name for a given value * @return false if the link set not contains a link with given value */ public boolean updateLink(double Value,String CatName){ Link SearchLink = new Link(Value,""); if(!Links.contains(SearchLink)) return false; Link L = (Link) Links.tailSet(SearchLink).first(); L.setCatName(CatName); return true; } /** exists a link to the given value ? */ public boolean exists(int Value){ Link SearchLink = new Link(Value,""); return Links.contains(SearchLink); } /** exists a link to the given value ? */ public boolean exists(double Value){ Link SearchLink = new Link(Value,""); return Links.contains(SearchLink); } /** exists a link to the given value ? */ public boolean exists(String Value){ Link SearchLink = new Link(Value,""); return Links.contains(SearchLink); } /** exists a link to the given value ? */ public boolean exists(boolean Value){ Link SearchLink = new Link(Value,""); return Links.contains(SearchLink); } /** returns the number of attribute in List value with existing * link to a Category */ public int numberOfLinksFor(ListExpr List){ ListExpr Values = List; int tno =0; while (!Values.isEmpty()){ ListExpr LE=Values.first(); Values = Values.rest(); if(getCatName(LE)!=null){ tno++; } } return tno; } /** Returns the name of the category for a given list. * The list must be one of the supported atom types. **/ public String getCatName(ListExpr LE){ int atomType = LE.atomType(); switch(atomType){ case ListExpr.SYMBOL_ATOM: return getCatName(LE.symbolValue()); case ListExpr.BOOL_ATOM: return getCatName(LE.boolValue()); case ListExpr.STRING_ATOM: return getCatName(LE.stringValue()); case ListExpr.REAL_ATOM: return getCatName(LE.realValue()); case ListExpr.INT_ATOM: return getCatName(LE.intValue()); default: return null; } } /** search the category name for given value * if this value don't exists in the set of * links the null is returned */ public String getCatName(String Value){ Link SearchLink = new Link(Value,""); SortedSet SS = Links.tailSet(SearchLink); if(SS.isEmpty()) return null; return ((Link)SS.first()).getCatName(); } /** search the category name for given value * if this value don't exists in the set of * links the null is returned */ public String getCatName(boolean Value){ Link SearchLink = new Link(Value,""); SortedSet SS = Links.tailSet(SearchLink); if(SS.isEmpty()) return null; return ((Link)SS.first()).getCatName(); } /** search the category name for given value * if this value don't exists in the set of * links the null is returned */ public String getCatName(int Value){ Link SearchLink = new Link(Value,""); SortedSet SS = Links.tailSet(SearchLink); if(SS.isEmpty()) return null; return ((Link)SS.first()).getCatName(); } /** search the category name for given value * if this value don't exists in the set of * links the null is returned */ public String getCatName(double Value){ Link SearchLink = new Link(Value,""); SortedSet SS = Links.tailSet(SearchLink); if(SS.isEmpty()) return null; return ((Link)SS.first()).getCatName(); } /** removes all links from this */ public void clear(){ Links.clear(); } /** returns the number of links */ public int getSize(){ return Links.size(); } /** returns the String represetation of the value at position index, * if this index don't exists, null is returned */ public String getValueStringAt(int index){ if(index <0 || index>=Links.size()) return null; Iterator it = Links.iterator(); Object o=null; for(int i=0;i=Links.size()) return null; Iterator it = Links.iterator(); Object o=null; for(int i=0;i=Links.size()||Name==null) return; Iterator it = Links.iterator(); Object o=null; for(int i=0;iL.type) return 1; if(type==INTTYPE){ if(IntValueL.IntValue) return 1; return 0; } if(type==REALTYPE){ if(DoubleValue < L.DoubleValue) return -1; if(DoubleValue > L.DoubleValue) return 1; return 0; } if(type==BOOLTYPE){ if(!BoolValue & L.BoolValue) return -1; if(BoolValue & !L.BoolValue) return 1; return 0; } if(type==STRINGTYPE){ return StringValue.compareTo(L.StringValue); } return -100; // this point should never be reached } } // class Link private class LinkComparator implements Comparator{ public int compare(Object o1,Object o2){ if( (o1 instanceof Link ) & (o2 instanceof Link)) return ((Link)o1).compareTo((Link)o2); else return -1; } } private class StringComparator implements Comparator{ public int compare(Object o1,Object o2){ if( (o1 instanceof String) && (o2 instanceof String)) return ((String)o1).compareTo((String)o2); else return -1; } } private final static int REALTYPE = 0; private final static int STRINGTYPE =1; private final static int INTTYPE = 2; private final static int BOOLTYPE = 3; } // class