/[cashew-s-editor]/cashews/src/nongnu/cashews/rdf/Blank.java
ViewVC logotype

Diff of /cashews/src/nongnu/cashews/rdf/Blank.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.2 by gnu_andrew, Sun Apr 17 18:35:32 2005 UTC revision 1.3 by gnu_andrew, Thu May 19 10:51:48 2005 UTC
# Line 21  Line 21 
21    
22  package nongnu.cashews.rdf;  package nongnu.cashews.rdf;
23    
24    import java.util.HashMap;
25    import java.util.HashSet;
26    import java.util.Map;
27    import java.util.Set;
28    
29  /**  /**
30   * <p>   * <p>
31   * Blank nodes allow different parts of an RDF graph to be connected,   * Blank nodes allow different parts of an RDF graph to be connected,
# Line 28  package nongnu.cashews.rdf; Line 33  package nongnu.cashews.rdf;
33   * node has an identifier, which allows it to be used and referenced   * node has an identifier, which allows it to be used and referenced
34   * in a triple.   * in a triple.
35   * </p>   * </p>
36     * <p>
37     * <strong>Note</strong>: Blank nodes are not created via a constructor.
38     * They are created via the methods <code>generateBlankNode()</code> and
39     * <code>generateBlankNode(String)</code> in order to ensure identifier
40     * uniqueness.
41     * </p>
42   *   *
43   * @author Andrew John Hughes (gnu_andrew@member.fsf.org)   * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
44   */   */
# Line 36  public class Blank Line 47  public class Blank
47  {  {
48    
49    /**    /**
50       * The map of graphs to blank node IDs.
51       */
52      private static Map<Graph,Set<String>> nodeIds;
53    
54      /**
55       * Static initializer for the blank node set.
56       */
57      static
58      {
59        nodeIds = new HashMap<Graph,Set<String>>();
60      }
61    
62      /**
63     * The identifier for this blank node.     * The identifier for this blank node.
64     *     *
65     * @serial the identifier for this node.     * @serial the identifier for this node.
# Line 43  public class Blank Line 67  public class Blank
67    private String id;    private String id;
68    
69    /**    /**
70     * Constructs a <code>Blank</code> using the specified identifier.     * Private constructor to prevent manual creation.
71       */
72      private Blank()
73      {
74      }
75    
76      /**
77       * Constructs a <code>Blank</code> node for the specified graph
78       *  using an automatically generated identifier.
79       *
80       * @param graph the graph of which the blank node will be a part.
81       * @return a <code>Blank</code> node.
82       */
83      public static Blank generateBlankNode(Graph graph)
84      {
85        boolean added = false;
86        Blank blank = new Blank();
87        String randomId;
88        do
89          {
90            randomId = Long.toString(Math.round(Math.random() * Long.MAX_VALUE));
91            added = blank.setIdentifier(graph, randomId);
92          } while (!added);
93        return blank;
94      }
95    
96      /**
97       * Constructs a <code>Blank</code> node for the specified graph
98       * using the supplied identifier.
99     *     *
100       * @param graph the graph of which the blank node will be a part.
101     * @param identifier the identifier for this blank node.     * @param identifier the identifier for this blank node.
102       * @return a <code>Blank</code> node or <code>null</code> if a
103       *         unique identifier could not be generated.
104     */     */
105    public Blank(String identifier)    public static Blank generateBlankNode(Graph graph, String identifier)
106    {    {
107      setIdentifier(identifier);      Blank blank = new Blank();
108        if (blank.setIdentifier(graph, identifier))
109          return blank;
110        return null;
111    }    }
112    
113    /**    /**
# Line 77  public class Blank Line 135  public class Blank
135    }    }
136    
137    /**    /**
138     * Sets the identifier used by the blank node to the one specified.     * Sets the identifier used by the blank node to be the one specified,
139       * provided the identifier is unique to the graph supplied.
140     *     *
141       * @param graph the graph of which the blank node will be a part.
142     * @param identifier the new identifier to use.     * @param identifier the new identifier to use.
143       * @return <code>true</code> if the identifier was changed.
144       */
145      public boolean setIdentifier(Graph graph, String identifier)
146      {
147        Set<String> idSet = nodeIds.get(graph);
148        if (idSet == null)
149          {
150            idSet = new HashSet<String>();
151            nodeIds.put(graph, idSet);
152          }
153        if (idSet.add(identifier))
154        {
155          id = identifier;
156          return true;
157        }
158        return false;
159      }
160    
161      /**
162       * Returns the identifier of this blank node.
163       *
164       * @return the blank node identifier.
165       */
166      public String getIdentifier()
167      {
168        /* Strings are immutable, so we just return it. */
169        return id;
170      }
171    
172      /**
173       * Returns true if the specified object is a blank node and
174       * is equal to this one.
175       *
176       * @param object the object to compare.
177       */
178      public boolean equals(Object obj)
179      {
180        if (super.equals(obj))
181          {
182            Blank blank = (Blank) obj;
183            return id.equals(blank.getIdentifier());
184          }
185        return false;
186      }
187    
188      /**
189       * Returns the hashcode of this blank node. This is calculated using
190       * the identifier's hash code and that of the super class.
191       *
192       * @return the hashcode for the blank node.
193     */     */
194    public void setIdentifier(String identifier)    public int hashCode()
195    {    {
196      id = identifier;      return super.hashCode() + 17 * id.hashCode();
197    }    }
198    
199  }  }

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.3

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26