View Javadoc

1   /*
2    * Copyright (c) 2008, the JUNG Project and the Regents of the University
3    * of California
4    * All rights reserved.
5    *
6    * This software is open-source under the BSD license; see either
7    * "license.txt" or
8    * http://jung.sourceforge.net/license.txt for a description.
9    */
10  
11  package edu.uci.ics.jung.io.graphml;
12  
13  import java.util.HashMap;
14  import java.util.Map;
15  
16  /**
17   * Metadata structure for the 'graph' GraphML element.
18   * 
19   * @author Nathan Mittler - nathan.mittler@gmail.com
20   * 
21   * @see "http://graphml.graphdrawing.org/specification.html"
22   */
23  public class GraphMetadata extends AbstractMetadata {
24  
25      public enum EdgeDefault {
26          DIRECTED, UNDIRECTED
27      }
28  
29      private String id;
30      private EdgeDefault edgeDefault;
31      private String description;
32      private Object graph;
33      final private Map<Object, NodeMetadata> nodes = new HashMap<Object, NodeMetadata>();
34      final private Map<Object, EdgeMetadata> edges = new HashMap<Object, EdgeMetadata>();
35      final private Map<Object, HyperEdgeMetadata> hyperEdges = new HashMap<Object, HyperEdgeMetadata>();
36  
37      public String getId() {
38          return id;
39      }
40  
41      public void setId(String id) {
42          this.id = id;
43      }
44  
45      public EdgeDefault getEdgeDefault() {
46          return edgeDefault;
47      }
48  
49      public void setEdgeDefault(EdgeDefault edgeDefault) {
50          this.edgeDefault = edgeDefault;
51      }
52  
53      public String getDescription() {
54          return description;
55      }
56  
57      public void setDescription(String desc) {
58          this.description = desc;
59      }
60  
61      public void addNodeMetadata(Object vertex, NodeMetadata metadata) {
62          nodes.put(vertex, metadata);
63      }
64  
65      public NodeMetadata getNodeMetadata(Object vertex) {
66          return nodes.get(vertex);
67      }
68  
69      public Map<Object, NodeMetadata> getNodeMap() {
70          return nodes;
71      }
72  
73      public void addEdgeMetadata(Object edge, EdgeMetadata metadata) {
74          edges.put(edge, metadata);
75      }
76  
77      public EdgeMetadata getEdgeMetadata(Object edge) {
78          return edges.get(edge);
79      }
80  
81      public Map<Object, EdgeMetadata> getEdgeMap() {
82          return edges;
83      }
84  
85      public void addHyperEdgeMetadata(Object edge, HyperEdgeMetadata metadata) {
86          hyperEdges.put(edge, metadata);
87      }
88  
89      public HyperEdgeMetadata getHyperEdgeMetadata(Object edge) {
90          return hyperEdges.get(edge);
91      }
92  
93      public Map<Object, HyperEdgeMetadata> getHyperEdgeMap() {
94          return hyperEdges;
95      }
96  
97      public Object getGraph() {
98          return graph;
99      }
100 
101     public void setGraph(Object graph) {
102         this.graph = graph;
103     }
104 
105     public MetadataType getMetadataType() {
106         return MetadataType.GRAPH;
107     }
108 
109     /**
110      * Gets the property for the given vertex object.
111      * 
112      * @param vertex
113      *            the subject vertex
114      * @param key
115      *            the property key
116      * @return the property value
117      * @throws IllegalArgumentException
118      *             thrown if there is no metadata associated with the provided
119      *             vertex object.
120      */
121     public String getVertexProperty(Object vertex, String key)
122             throws IllegalArgumentException {
123         NodeMetadata metadata = getNodeMetadata(vertex);
124         if (metadata == null) {
125             throw new IllegalArgumentException(
126                     "Metadata does not exist for provided vertex");
127         }
128 
129         return metadata.getProperty(key);
130     }
131 
132     /**
133      * Gets the property for the given edge object.
134      * 
135      * @param edge
136      *            the subject edge.
137      * @param key
138      *            the property key
139      * @return the property value
140      * @throws IllegalArgumentException
141      *             thrown if there is no metadata associated with the provided
142      *             edge object.
143      */
144     public String getEdgeProperty(Object edge, String key)
145             throws IllegalArgumentException {
146 
147         // First, try standard edges.
148         EdgeMetadata em = getEdgeMetadata(edge);
149         if (em != null) {
150             return em.getProperty(key);
151         }
152 
153         // Next, try hyperedges.
154         HyperEdgeMetadata hem = getHyperEdgeMetadata(edge);
155         if (hem != null) {
156             return hem.getProperty(key);
157         }
158 
159         // Couldn't find the edge.
160         throw new IllegalArgumentException(
161                 "Metadata does not exist for provided edge");
162     }
163 
164 }