@Test public void testMethod() { Assert.assertEquals(ALICE, Factory.instantiate( ImmutableMap.<String, Object>of("name", "Alice", "age", 84), new URIImpl(FactoryTest.BASE + "#create"), Object.class)); }
public static Map<URI, Object> instantiate(final Iterable<? extends Statement> model, final URI... ids) { final Map<Resource, URI> types = Maps.newLinkedHashMap(); final Multimap<Resource, Statement> stmt = ArrayListMultimap.create(); for (final Statement statement : model) { final Resource s = statement.getSubject(); final URI p = statement.getPredicate(); final Value o = statement.getObject(); if (p.equals(RDF.TYPE) && o instanceof URI && o.stringValue().startsWith(SCHEME)) { types.put(s, (URI) o); } else if (p.stringValue().startsWith(SCHEME)) { stmt.put(s, statement); } } if (ids != null && ids.length > 0) { final Set<Resource> subjs = Sets.<Resource>newHashSet(ids); int size; do { size = subjs.size(); for (final Statement statement : stmt.values()) { final Value o = statement.getObject(); if (o instanceof Resource) { subjs.add((Resource) o); } } } while (subjs.size() > size); types.keySet().retainAll(subjs); } final Map<Resource, Object> map = Maps.newHashMap(); while (!types.isEmpty()) { final int size = types.size(); for (final Resource s : Lists.newArrayList(types.keySet())) { final Collection<Statement> statements = stmt.get(s); boolean dependent = false; for (final Statement statement : statements) { final Value o = statement.getObject(); if (o instanceof Resource && types.keySet().contains(o)) { dependent = true; break; } } if (!dependent) { final URI implementation = types.get(s); final Multimap<String, Object> properties = ArrayListMultimap.create(); for (final Statement statement : statements) { final URI p = statement.getPredicate(); final Value o = statement.getObject(); final Object obj = map.get(o); properties.put(p.stringValue().substring(SCHEME.length()), obj != null ? obj : o); } Preconditions.checkArgument(implementation != null, "No implementation specified for %s", s); map.put(s, instantiate(properties.asMap(), implementation, Object.class)); types.remove(s); } } Preconditions.checkArgument(types.size() < size, "Cannot instantiate " + stmt.keySet() + " - detected circular dependencies"); } final ImmutableMap.Builder<URI, Object> builder = ImmutableMap.builder(); for (final Map.Entry<Resource, Object> entry : map.entrySet()) { final Resource s = entry.getKey(); final Object obj = entry.getValue(); if (s instanceof URI && (ids == null || ids.length == 0 || Arrays.asList(ids).contains(s))) { builder.put((URI) s, obj); } } return builder.build(); }
Factory { public static Map<URI, Object> instantiate(final Iterable<? extends Statement> model, final URI... ids) { final Map<Resource, URI> types = Maps.newLinkedHashMap(); final Multimap<Resource, Statement> stmt = ArrayListMultimap.create(); for (final Statement statement : model) { final Resource s = statement.getSubject(); final URI p = statement.getPredicate(); final Value o = statement.getObject(); if (p.equals(RDF.TYPE) && o instanceof URI && o.stringValue().startsWith(SCHEME)) { types.put(s, (URI) o); } else if (p.stringValue().startsWith(SCHEME)) { stmt.put(s, statement); } } if (ids != null && ids.length > 0) { final Set<Resource> subjs = Sets.<Resource>newHashSet(ids); int size; do { size = subjs.size(); for (final Statement statement : stmt.values()) { final Value o = statement.getObject(); if (o instanceof Resource) { subjs.add((Resource) o); } } } while (subjs.size() > size); types.keySet().retainAll(subjs); } final Map<Resource, Object> map = Maps.newHashMap(); while (!types.isEmpty()) { final int size = types.size(); for (final Resource s : Lists.newArrayList(types.keySet())) { final Collection<Statement> statements = stmt.get(s); boolean dependent = false; for (final Statement statement : statements) { final Value o = statement.getObject(); if (o instanceof Resource && types.keySet().contains(o)) { dependent = true; break; } } if (!dependent) { final URI implementation = types.get(s); final Multimap<String, Object> properties = ArrayListMultimap.create(); for (final Statement statement : statements) { final URI p = statement.getPredicate(); final Value o = statement.getObject(); final Object obj = map.get(o); properties.put(p.stringValue().substring(SCHEME.length()), obj != null ? obj : o); } Preconditions.checkArgument(implementation != null, "No implementation specified for %s", s); map.put(s, instantiate(properties.asMap(), implementation, Object.class)); types.remove(s); } } Preconditions.checkArgument(types.size() < size, "Cannot instantiate " + stmt.keySet() + " - detected circular dependencies"); } final ImmutableMap.Builder<URI, Object> builder = ImmutableMap.builder(); for (final Map.Entry<Resource, Object> entry : map.entrySet()) { final Resource s = entry.getKey(); final Object obj = entry.getValue(); if (s instanceof URI && (ids == null || ids.length == 0 || Arrays.asList(ids).contains(s))) { builder.put((URI) s, obj); } } return builder.build(); } }
Factory { public static Map<URI, Object> instantiate(final Iterable<? extends Statement> model, final URI... ids) { final Map<Resource, URI> types = Maps.newLinkedHashMap(); final Multimap<Resource, Statement> stmt = ArrayListMultimap.create(); for (final Statement statement : model) { final Resource s = statement.getSubject(); final URI p = statement.getPredicate(); final Value o = statement.getObject(); if (p.equals(RDF.TYPE) && o instanceof URI && o.stringValue().startsWith(SCHEME)) { types.put(s, (URI) o); } else if (p.stringValue().startsWith(SCHEME)) { stmt.put(s, statement); } } if (ids != null && ids.length > 0) { final Set<Resource> subjs = Sets.<Resource>newHashSet(ids); int size; do { size = subjs.size(); for (final Statement statement : stmt.values()) { final Value o = statement.getObject(); if (o instanceof Resource) { subjs.add((Resource) o); } } } while (subjs.size() > size); types.keySet().retainAll(subjs); } final Map<Resource, Object> map = Maps.newHashMap(); while (!types.isEmpty()) { final int size = types.size(); for (final Resource s : Lists.newArrayList(types.keySet())) { final Collection<Statement> statements = stmt.get(s); boolean dependent = false; for (final Statement statement : statements) { final Value o = statement.getObject(); if (o instanceof Resource && types.keySet().contains(o)) { dependent = true; break; } } if (!dependent) { final URI implementation = types.get(s); final Multimap<String, Object> properties = ArrayListMultimap.create(); for (final Statement statement : statements) { final URI p = statement.getPredicate(); final Value o = statement.getObject(); final Object obj = map.get(o); properties.put(p.stringValue().substring(SCHEME.length()), obj != null ? obj : o); } Preconditions.checkArgument(implementation != null, "No implementation specified for %s", s); map.put(s, instantiate(properties.asMap(), implementation, Object.class)); types.remove(s); } } Preconditions.checkArgument(types.size() < size, "Cannot instantiate " + stmt.keySet() + " - detected circular dependencies"); } final ImmutableMap.Builder<URI, Object> builder = ImmutableMap.builder(); for (final Map.Entry<Resource, Object> entry : map.entrySet()) { final Resource s = entry.getKey(); final Object obj = entry.getValue(); if (s instanceof URI && (ids == null || ids.length == 0 || Arrays.asList(ids).contains(s))) { builder.put((URI) s, obj); } } return builder.build(); }  }
Factory { public static Map<URI, Object> instantiate(final Iterable<? extends Statement> model, final URI... ids) { final Map<Resource, URI> types = Maps.newLinkedHashMap(); final Multimap<Resource, Statement> stmt = ArrayListMultimap.create(); for (final Statement statement : model) { final Resource s = statement.getSubject(); final URI p = statement.getPredicate(); final Value o = statement.getObject(); if (p.equals(RDF.TYPE) && o instanceof URI && o.stringValue().startsWith(SCHEME)) { types.put(s, (URI) o); } else if (p.stringValue().startsWith(SCHEME)) { stmt.put(s, statement); } } if (ids != null && ids.length > 0) { final Set<Resource> subjs = Sets.<Resource>newHashSet(ids); int size; do { size = subjs.size(); for (final Statement statement : stmt.values()) { final Value o = statement.getObject(); if (o instanceof Resource) { subjs.add((Resource) o); } } } while (subjs.size() > size); types.keySet().retainAll(subjs); } final Map<Resource, Object> map = Maps.newHashMap(); while (!types.isEmpty()) { final int size = types.size(); for (final Resource s : Lists.newArrayList(types.keySet())) { final Collection<Statement> statements = stmt.get(s); boolean dependent = false; for (final Statement statement : statements) { final Value o = statement.getObject(); if (o instanceof Resource && types.keySet().contains(o)) { dependent = true; break; } } if (!dependent) { final URI implementation = types.get(s); final Multimap<String, Object> properties = ArrayListMultimap.create(); for (final Statement statement : statements) { final URI p = statement.getPredicate(); final Value o = statement.getObject(); final Object obj = map.get(o); properties.put(p.stringValue().substring(SCHEME.length()), obj != null ? obj : o); } Preconditions.checkArgument(implementation != null, "No implementation specified for %s", s); map.put(s, instantiate(properties.asMap(), implementation, Object.class)); types.remove(s); } } Preconditions.checkArgument(types.size() < size, "Cannot instantiate " + stmt.keySet() + " - detected circular dependencies"); } final ImmutableMap.Builder<URI, Object> builder = ImmutableMap.builder(); for (final Map.Entry<Resource, Object> entry : map.entrySet()) { final Resource s = entry.getKey(); final Object obj = entry.getValue(); if (s instanceof URI && (ids == null || ids.length == 0 || Arrays.asList(ids).contains(s))) { builder.put((URI) s, obj); } } return builder.build(); }  static Map<URI, Object> instantiate(final Iterable<? extends Statement> model,
            final URI... ids); static T instantiate(final Iterable<? extends Statement> model, final URI id,
            final Class<T> type); static T instantiate(final Map<String, ? extends Object> properties,
            final URI implementation, final Class<T> type); }
Factory { public static Map<URI, Object> instantiate(final Iterable<? extends Statement> model, final URI... ids) { final Map<Resource, URI> types = Maps.newLinkedHashMap(); final Multimap<Resource, Statement> stmt = ArrayListMultimap.create(); for (final Statement statement : model) { final Resource s = statement.getSubject(); final URI p = statement.getPredicate(); final Value o = statement.getObject(); if (p.equals(RDF.TYPE) && o instanceof URI && o.stringValue().startsWith(SCHEME)) { types.put(s, (URI) o); } else if (p.stringValue().startsWith(SCHEME)) { stmt.put(s, statement); } } if (ids != null && ids.length > 0) { final Set<Resource> subjs = Sets.<Resource>newHashSet(ids); int size; do { size = subjs.size(); for (final Statement statement : stmt.values()) { final Value o = statement.getObject(); if (o instanceof Resource) { subjs.add((Resource) o); } } } while (subjs.size() > size); types.keySet().retainAll(subjs); } final Map<Resource, Object> map = Maps.newHashMap(); while (!types.isEmpty()) { final int size = types.size(); for (final Resource s : Lists.newArrayList(types.keySet())) { final Collection<Statement> statements = stmt.get(s); boolean dependent = false; for (final Statement statement : statements) { final Value o = statement.getObject(); if (o instanceof Resource && types.keySet().contains(o)) { dependent = true; break; } } if (!dependent) { final URI implementation = types.get(s); final Multimap<String, Object> properties = ArrayListMultimap.create(); for (final Statement statement : statements) { final URI p = statement.getPredicate(); final Value o = statement.getObject(); final Object obj = map.get(o); properties.put(p.stringValue().substring(SCHEME.length()), obj != null ? obj : o); } Preconditions.checkArgument(implementation != null, "No implementation specified for %s", s); map.put(s, instantiate(properties.asMap(), implementation, Object.class)); types.remove(s); } } Preconditions.checkArgument(types.size() < size, "Cannot instantiate " + stmt.keySet() + " - detected circular dependencies"); } final ImmutableMap.Builder<URI, Object> builder = ImmutableMap.builder(); for (final Map.Entry<Resource, Object> entry : map.entrySet()) { final Resource s = entry.getKey(); final Object obj = entry.getValue(); if (s instanceof URI && (ids == null || ids.length == 0 || Arrays.asList(ids).contains(s))) { builder.put((URI) s, obj); } } return builder.build(); }  static Map<URI, Object> instantiate(final Iterable<? extends Statement> model,
            final URI... ids); static T instantiate(final Iterable<? extends Statement> model, final URI id,
            final Class<T> type); static T instantiate(final Map<String, ? extends Object> properties,
            final URI implementation, final Class<T> type);  }