public void testCacheCreation(String type) {
   Cache c1 = CacheManager.createCache(type, "c1", 100);
   try {
     Cache c2 = CacheManager.createCache(type, "c2", 1);
     assertTrue("Missing error on bad cache size: " + type, false);
   } catch (Error e) {
   }
 }
    public void testFillTheCache(String type) {
      final int size = 100;
      Cache c1 = CacheManager.createCache(type, "c1", size);
      String[] k = new String[size];
      String[] v = new String[size];

      for (int i = 0; i < size; i++) {
        k[i] = "K" + i;
        v[i] = "V" + i;
        c1.put(k[i], v[i]);
      }

      int count = 0;

      for (int i = 0; i < size; i++) {
        if (c1.get(k[i]) != null) {
          count++;
        }
      }

      assertTrue("too low a hit rate: " + type + " = " + count, count > size / 2);
      assertEquals("count puts", size, c1.getPuts());
      assertEquals("count gets", size, c1.getGets());
      assertEquals("count hits", count, c1.getHits());
    }
    public void testCacheSimpleReturn(String type) {

      int size = 100;
      // this test does not fill the cache
      Cache c1 = CacheManager.createCache(type, "c1", size);

      String k1 = "one";
      String k2 = k1;
      String k3 = k2;
      Integer v1 = new Integer(-1);
      Integer v2 = v1;
      Integer v3 = v2;
      c1.put(k1, v1);

      for (int i = 0; i < size; i++) {
        k1 = k2;
        v1 = v2;
        Object o = c1.get(k1);
        assertTrue("expected a hit", o != null);
        assertEquals("should be the expected object", o, v1);
        k2 = k3;
        v2 = v3;
        o = c1.get(k2);
        assertTrue("expected a hit", o != null);
        assertEquals("should be the expected object", o, v2);

        k3 = "T" + i;
        v3 = new Integer(i);
        c1.put(k3, v3);
      }
    }
Exemple #4
0
/**
 * A specialisation of Polymorphic that models an extended graph - that is, one that contains{@link
 * EnhNode Enhanced nodes} or one that itself exposes additional capabilities beyond the graph API.
 *
 * <p><span style="color:red">WARNING</span>. The polymorphic aspects of EnhGraph are <span
 * style="color:red">not supported</span> and are not expected to be supported in this way for the
 * indefinite future.
 *
 * @author <a href="mailto:[email protected]">Jeremy Carroll</a> (original code) <br>
 *     <a href="mailto:[email protected]">Chris Dollin</a> (original code) <br>
 *     <a href="mailto:[email protected]">Ian Dickinson</a> (refactoring and commentage)
 */
public class EnhGraph extends Polymorphic {
  // Instance variables
  /** The graph that this enhanced graph is wrapping */
  protected Graph graph;

  /** Counter that helps to ensure that caches are kept distinct */
  private static int cnt = 0;

  /** Cache of enhanced nodes that have been created */
  protected Cache enhNodes =
      CacheManager.createCache(CacheManager.ENHNODECACHE, "EnhGraph-" + cnt++, 1000);

  /** The unique personality that is bound to this polymorphic instace */
  private Personality personality;

  public boolean isValid() {
    return true;
  }

  // Constructors
  /**
   * Construct an enhanced graph from the given underlying graph, and a factory for generating
   * enhanced nodes.
   *
   * @param g The underlying plain graph, may be null to defer binding to a given graph until later.
   * @param p The personality factory, that maps types to realisations
   */
  public EnhGraph(Graph g, Personality p) {
    super();
    graph = g;
    personality = p;
  }

  // External methods

  /**
   * Answer the normal graph that this enhanced graph is wrapping.
   *
   * @return A graph
   */
  public Graph asGraph() {
    return graph;
  }

  /**
   * Hashcode for an enhnaced graph is delegated to the underlyin graph.
   *
   * @return The hashcode as an int
   */
  public final int hashCode() {
    return graph.hashCode();
  }

  /**
   * An enhanced graph is equal to another graph g iff the underlying graphs are equal. This is
   * deemed to be a complete and correct interpretation of enhanced graph equality, which is why
   * this method has been marked final.
   *
   * <p>Note that this equality test does not look for correspondance between the structures in the
   * two graphs. To test whether another graph has the same nodes and edges as this one, use {@link
   * #isIsomorphicWith}.
   *
   * @param o An object to test for equality with this node
   * @return True if o is equal to this node.
   * @see #isIsomorphicWith
   */
  public final boolean equals(Object o) {
    return this == o || o instanceof EnhGraph && graph.equals(((EnhGraph) o).asGraph());
  }

  /**
   * Answer true if the given enhanced graph contains the same nodes and edges as this graph. The
   * default implementation delegates this to the underlying graph objects.
   *
   * @param eg A graph to test
   * @return True if eg is a graph with the same structure as this.
   */
  public final boolean isIsomorphicWith(EnhGraph eg) {
    return graph.isIsomorphicWith(eg.graph);
  }

  /**
   * Answer an enhanced node that wraps the given node and conforms to the given interface type.
   *
   * @param n A node (assumed to be in this graph)
   * @param interf A type denoting the enhanced facet desired
   * @return An enhanced node
   */
  public EnhNode getNodeAs(Node n, Class interf) {
    // We use a cache to avoid reconstructing the same Node too many times.
    EnhNode eh = (EnhNode) enhNodes.get(n);
    if (eh != null) return eh.viewAs(interf);

    // not in the cache, so build a new one
    eh = (EnhNode) ((GraphPersonality) personality).nodePersonality().newInstance(interf, n, this);
    enhNodes.put(n, eh);
    return eh;
  }

  /**
   * Answer the cache controlle for this graph
   *
   * @return A cache controller object
   */
  public CacheControl getNodeCacheControl() {
    return enhNodes;
  }

  /**
   * Set the cache controller object for this graph
   *
   * @param cc The cache controller
   */
  public void setNodeCache(Cache cc) {
    enhNodes = cc;
  }

  /**
   * Answer an enhanced graph that presents <i>this</i> in a way which satisfies type t. This is a
   * stub method that has not yet been implemented.
   *
   * @deprecated
   * @param t A type
   * @return A polymorphic instance, possibly but not necessarily this, that conforms to t.
   */
  protected Polymorphic convertTo(Class t) {
    throw new PersonalityConfigException(
        "Alternative perspectives on graphs has not been implemented yet");
  }

  /**
   * we can't convert to anything.
   *
   * @deprecated
   */
  protected boolean canSupport(Class t) {
    return false;
  }

  /**
   * Answer the personality object bound to this polymorphic instance
   *
   * @return The personality object
   */
  protected Personality getPersonality() {
    return personality;
  }
}