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.ArrayList;
14  import java.util.HashMap;
15  import java.util.List;
16  import java.util.Map;
17  import java.util.Set;
18  
19  /**
20   * A KeyMap is a storage mechanism for the keys read from the GraphML file. It
21   * stores the keys indexed by the type of GraphML metadata (node, edge, etc)
22   * that the key applies to. The <code>applyKeys</code> method will obtain the
23   * list of keys that apply to the given metadata type and apply the keys
24   * one-by-one to the metadata.
25   *
26   * @author Nathan Mittler - nathan.mittler@gmail.com
27   */
28  public class KeyMap {
29  
30      final private Map<Metadata.MetadataType, List<Key>> map = new HashMap<Metadata.MetadataType, List<Key>>();
31  
32      /**
33       * Adds the given key to the map.
34       *
35       * @param key the key to be added.
36       */
37      public void addKey(Key key) {
38  
39          switch (key.getForType()) {
40              case EDGE: {
41                  getKeyList(Metadata.MetadataType.EDGE).add(key);
42                  break;
43              }
44              case ENDPOINT: {
45                  getKeyList(Metadata.MetadataType.ENDPOINT).add(key);
46                  break;
47              }
48              case GRAPH: {
49                  getKeyList(Metadata.MetadataType.GRAPH).add(key);
50                  break;
51              }
52              case HYPEREDGE: {
53                  getKeyList(Metadata.MetadataType.HYPEREDGE).add(key);
54                  break;
55              }
56              case NODE: {
57                  getKeyList(Metadata.MetadataType.NODE).add(key);
58                  break;
59              }
60              case PORT: {
61                  getKeyList(Metadata.MetadataType.PORT).add(key);
62                  break;
63              }
64              default: {
65  
66                  // Default = ALL
67                  getKeyList(Metadata.MetadataType.EDGE).add(key);
68                  getKeyList(Metadata.MetadataType.ENDPOINT).add(key);
69                  getKeyList(Metadata.MetadataType.GRAPH).add(key);
70                  getKeyList(Metadata.MetadataType.HYPEREDGE).add(key);
71                  getKeyList(Metadata.MetadataType.NODE).add(key);
72                  getKeyList(Metadata.MetadataType.PORT).add(key);
73              }
74          }
75      }
76  
77      /**
78       * Applies all keys that are applicable to the given metadata.
79       *
80       * @param metadata the target metadata.
81       */
82      public void applyKeys(Metadata metadata) {
83  
84          List<Key> keys = getKeyList(metadata.getMetadataType());
85          for (Key key : keys) {
86              key.applyKey(metadata);
87          }
88      }
89  
90      /**
91       * Clears this map.
92       */
93      public void clear() {
94          map.clear();
95      }
96  
97      /**
98       * Retrieves the set of entries contained in this map.
99       *
100      * @return all of the entries in this map.
101      */
102     public Set<Map.Entry<Metadata.MetadataType, List<Key>>> entrySet() {
103         return map.entrySet();
104     }
105 
106     /**
107      * Gets the list for the given metadata type. If doesn't exist, the list is
108      * created.
109      *
110      * @param type the metadata type.
111      * @return the list for the metadata type.
112      */
113     private List<Key> getKeyList(Metadata.MetadataType type) {
114 
115         List<Key> keys = map.get(type);
116         if (keys == null) {
117             keys = new ArrayList<Key>();
118             map.put(type, keys);
119         }
120 
121         return keys;
122     }
123 }