/** * Provides a message for use with exceptions when no valid constructor is * found. The message is built using the suggestions from {@link * #suggest(Class)}. * * @param key the constructor key to build the message for * @return a message listing possible constructors */ private String getSuggestionMessage(ConstructorKey key) { StringBuilder sb = new StringBuilder(200); sb.append("No constructor found for '").append(key); sb.append("' candidates are: "); Iterator<ConstructorKey> iterator = suggest(key.intf()); while (iterator.hasNext()) { sb.append(iterator.next()); if (iterator.hasNext()) { sb.append(", "); } } return sb.toString(); }
/** * Provides a message for use with exceptions when no valid constructor is * found. The message is built using the suggestions from {@link * #suggest(Class)}. * * @param key the constructor key to build the message for * @return a message listing possible constructors */ private String getSuggestionMessage(ConstructorKey key) { StringBuilder sb = new StringBuilder(200); sb.append("No constructor found for '").append(key); sb.append("' candidates are: "); Iterator<ConstructorKey> iterator = suggest(key.intf()); while (iterator.hasNext()) { sb.append(iterator.next()); if (iterator.hasNext()) { sb.append(", "); } } return sb.toString(); }
/** *{@inheritDoc} */ @Override public String toString() { StringBuilder sb = new StringBuilder(n() * 50); sb.append(intf().getSimpleName()); sb.append('('); int max = n() - 1; for (int i = 0; i <= max; i++) { sb.append(type(i).getSimpleName()); if (i != max) sb.append(", "); } sb.append(')'); return sb.toString(); }
final ConstructorKey alt = new ObjectBasedKey(key.intf(), new Object[]{types});
final ConstructorKey alt = new ObjectBasedKey(key.intf(), new Object[]{types});
/** * Register a constructor key with a defined {@link Creator}. The key * defines the interface and parameters of the creation and the creator * actually creates the object. * * <pre>{@code * // import static org.openscience.cdk.DynamicFactory.key; * factory.register(key(IBond.class, IAtom[].class), * new BasicCreator<IAtom>(null) { * public IAtom create(Object[] objects) { * return new Bond((IAtom[]) objects); * } * }); * }</pre> * * @param key construction key, defines interface and parameter types * @param creator creates the actual object * @param <T> type of object that will be created * @return the registered creator - null if not registered */ public <T> Creator<T> register(ConstructorKey key, Creator<T> creator) { if (creator == null) return null; // make sure we don't register a constructor over an existing key if (cache.containsKey(key)) throw new IllegalArgumentException("cannot register " + key + " suppressed " + cache.get(key)); lookup.put(key.intf(), key); cache.put(key, creator); return creator; }
/** * Register a constructor key with a defined {@link Creator}. The key * defines the interface and parameters of the creation and the creator * actually creates the object. * * <pre>{@code * // import static org.openscience.cdk.DynamicFactory.key; * factory.register(key(IBond.class, IAtom[].class), * new BasicCreator<IAtom>(null) { * public IAtom create(Object[] objects) { * return new Bond((IAtom[]) objects); * } * }); * }</pre> * * @param key construction key, defines interface and parameter types * @param creator creates the actual object * @param <T> type of object that will be created * @return the registered creator - null if not registered */ public <T> Creator<T> register(ConstructorKey key, Creator<T> creator) { if (creator == null) return null; // make sure we don't register a constructor over an existing key if (cache.containsKey(key)) throw new IllegalArgumentException("cannot register " + key + " suppressed " + cache.get(key)); lookup.put(key.intf(), key); cache.put(key, creator); return creator; }
/** *{@inheritDoc} */ @Override public String toString() { StringBuilder sb = new StringBuilder(n() * 50); sb.append(intf().getSimpleName()); sb.append('('); int max = n() - 1; for (int i = 0; i <= max; i++) { sb.append(type(i).getSimpleName()); if (i != max) sb.append(", "); } sb.append(')'); return sb.toString(); }
/** *{@inheritDoc} */ @Override public boolean equals(Object o) { if (o == null || !(o instanceof ConstructorKey)) return false; ConstructorKey that = (ConstructorKey) o; if (intf() != that.intf() || n() != that.n()) { return false; } for (int i = 0; i < n(); i++) { if (!type(i).equals(that.type(i))) return false; } return true; }
/** * Ensures primitive types are converted. */ @Test public void testKey_Primitives() { DynamicFactory.ConstructorKey key = DynamicFactory.key(IAtom.class, boolean.class, byte.class, char.class, short.class, int.class, float.class, long.class, double.class); assertEquals(IAtom.class, key.intf()); assertEquals(8, key.n()); assertEquals(Boolean.class, key.type(0)); assertEquals(Byte.class, key.type(1)); assertEquals(Character.class, key.type(2)); assertEquals(Short.class, key.type(3)); assertEquals(Integer.class, key.type(4)); assertEquals(Float.class, key.type(5)); assertEquals(Long.class, key.type(6)); assertEquals(Double.class, key.type(7)); }
/** *{@inheritDoc} */ @Override public boolean equals(Object o) { if (o == null || !(o instanceof ConstructorKey)) return false; ConstructorKey that = (ConstructorKey) o; if (intf() != that.intf() || n() != that.n()) { return false; } for (int i = 0; i < n(); i++) { if (!type(i).equals(that.type(i))) return false; } return true; }
@Test public void testKey_ArrayParameters() { DynamicFactory.ConstructorKey key = DynamicFactory.key(IBond.class, IAtom[].class); assertEquals(IBond.class, key.intf()); assertEquals(1, key.n()); assertTrue(key.type(0).isArray()); assertEquals(IAtom[].class, key.type(0)); }
@Test public void testKey_Parameters() { DynamicFactory.ConstructorKey key = DynamicFactory.key(IBond.class, IAtom.class, IAtom.class); assertEquals(IBond.class, key.intf()); assertEquals(2, key.n()); assertEquals(IAtom.class, key.type(0)); assertEquals(IAtom.class, key.type(1)); }
/** *{@inheritDoc} */ @Override public int hashCode() { int result = intf().hashCode(); for (int i = 0; i < n(); i++) result = 31 * result + type(i).hashCode(); return result; }
/** *{@inheritDoc} */ @Override public int hashCode() { int result = intf().hashCode(); for (int i = 0; i < n(); i++) result = 31 * result + type(i).hashCode(); return result; }
/** * Access a set of candidates for a given key. Candidates match the * interface an number of parameters for the constructors. A key may * match when it's parameters are subclasses. * * @param key the key to find possible candidates for * @return set of constructors which 'could' match the given key */ public Set<ConstructorKey> getCandidates(ConstructorKey key) { return getCandidates(key.intf(), key.n()); }
@Test public void testKey_Default() { DynamicFactory.ConstructorKey key = DynamicFactory.key(IAtom.class); assertEquals(IAtom.class, key.intf()); assertEquals(0, key.n()); }
/** * Access a set of candidates for a given key. Candidates match the * interface an number of parameters for the constructors. A key may * match when it's parameters are subclasses. * * @param key the key to find possible candidates for * @return set of constructors which 'could' match the given key */ public Set<ConstructorKey> getCandidates(ConstructorKey key) { return getCandidates(key.intf(), key.n()); }