00001 package cz.vutbr.fit.dudka.SGVis.Data;
00002
00011 import java.net.URL;
00012 import java.util.Iterator;
00013
00019 public class RelationStorage implements
00020 Cloneable,
00021 IContainer<Relation>
00022 {
00023 private RelationSet all;
00024 private UrlSet urlSet;
00025 private UrlMap<String> urlToHostMap;
00026 private RelationMap<URL> fromMap;
00027 private RelationMap<URL> toMap;
00028 private RelationMap<String> edgeTypeMap;
00029
00035 public static RelationStorage fromRelationList(RelationList relationList) {
00036 RelationStorage relationSet = new RelationStorage();
00037 relationSet.add(relationList);
00038 return relationSet;
00039 }
00040
00046 public static String urlToHost(URL url) {
00047 return url.getHost();
00048 }
00049
00050 public RelationStorage() {
00051 all = new RelationSet();
00052 urlSet = new UrlSet();
00053 urlToHostMap = new UrlMap<String>();
00054 fromMap = new RelationMap<URL>();
00055 toMap = new RelationMap<URL>();
00056 edgeTypeMap = new RelationMap<String>();
00057 }
00058
00063 public RelationStorage(RelationStorage ref) {
00064 all = new RelationSet(ref.all);
00065 urlSet = new UrlSet(ref.urlSet);
00066 urlToHostMap = new UrlMap<String>(ref.urlToHostMap);
00067 fromMap = new RelationMap<URL>(ref.fromMap);
00068 toMap = new RelationMap<URL>(ref.toMap);
00069 edgeTypeMap = new RelationMap<String>(ref.edgeTypeMap);
00070 }
00071
00075 public RelationStorage clone() throws CloneNotSupportedException {
00076 return new RelationStorage(this);
00077 }
00078
00083 public void add(Relation relation) {
00084 all.add(relation);
00085 addUrl(relation.from);
00086 addUrl(relation.to);
00087 fromMap.addToSet(relation.from, relation);
00088 toMap.addToSet(relation.to, relation);
00089 edgeTypeMap.addToSet(relation.edgeType, relation);
00090 }
00091
00096 public void add(Iterable<Relation> relationList) {
00097 for(Relation relation: relationList)
00098 this.add(relation);
00099 }
00100
00106 public IContainer<Relation> getAllFrom(URL from) {
00107 return fromMap.get(from);
00108 }
00109
00115 public IContainer<Relation> getAllTo(URL to) {
00116 return toMap.get(to);
00117 }
00118
00124 public IContainer<Relation> getAllIncidents(URL url) {
00125 RelationList list = new RelationList();
00126 list.add(getAllFrom(url));
00127 list.add(getAllTo(url));
00128 return list;
00129 }
00130
00136 public IContainer<Relation> getAllOfEdgeType(String edgeType) {
00137 return edgeTypeMap.get(edgeType);
00138 }
00139
00140 public Iterator<Relation> iterator() {
00141 return all.iterator();
00142 }
00143
00147 public Iterable<URL> getUrls() {
00148 return urlSet;
00149 }
00150
00156 public Iterable<URL> getUrls(String host) {
00157 return urlToHostMap.get(host);
00158 }
00159
00163 public Iterable<String> getHosts() {
00164 return urlToHostMap.getKeys();
00165 }
00166
00171 public Statistics getStatistics() {
00172 int nHosts = 0;
00173 for (@SuppressWarnings("unused")
00174 String s:urlToHostMap.getKeys())
00175 nHosts++;
00176 return new Statistics(
00177 nHosts,
00178 urlSet.size(),
00179 all.size());
00180 }
00181 private void addUrl(URL url) {
00182 urlSet.add(url);
00183 urlToHostMap.addToSet(urlToHost(url), url);
00184 }
00185 }