private Object readResolve() { // Look up the name to ensure singletons // This is not entirely safe; see Effective Java, 2nd Ed., #77 for details. // However, we do not depend on singletons for security, only for correctness. // It is acceptable if malicious serialization streams result in broken objects. return forName(name); }
@Nullable @Override public String apply(@Nullable EntityType input) { return input == null ? null : input.getName(); } };
/** * Get the entity type for a name. * @param name The type name. * @return The entity type with name `name`. */ @Nonnull @JsonCreator public static EntityType forName(String name) { String normedName = name.toLowerCase(Locale.ROOT); EntityType type = new EntityType(normedName); EntityType canonical = TYPE_CACHE.putIfAbsent(normedName, type); return canonical == null ? type : canonical; } }
@Test public void testCreate() { EntityType wombat = EntityType.forName("wombat"); EntityType wombat2 = EntityType.forName("wombat"); EntityType woozle = EntityType.forName("woozle"); assertThat(wombat.getName(), equalTo("wombat")); assertThat(woozle.getName(), equalTo("woozle")); assertThat(wombat, equalTo(wombat2)); assertThat(wombat, sameInstance(wombat2)); assertThat(wombat, not(equalTo(woozle))); }
/** * Parse an entity derivation from a JSON node. * @param node The JSON node. * @return The entity derivation. */ public static EntityDerivation fromJSON(JsonNode node) { JsonNode src = node.get("source_type"); Preconditions.checkArgument(src != null, "missing source_type"); JsonNode tgt = node.get("entity_type"); Preconditions.checkArgument(tgt != null, "missing entity_type"); JsonNode attr = node.get("source_attribute"); Preconditions.checkArgument(attr != null, "missing source_attribute"); return create(EntityType.forName(tgt.asText()), EntityType.forName(src.asText()), TypedName.create(attr.asText(), Long.class)); } }
@Override public void describeTo(DescriptionWriter writer) { for (EntityType etype: Ordering.natural() .onResultOf(Entities.entityTypeNameFunction()) .sortedCopy(storage.keySet())) { writer.putField(etype.getName(), storage.get(etype)); } } }
/** * Get the entity type for a name. * @param name The type name. * @return The entity type with name `name`. */ @Nonnull @JsonCreator public static EntityType forName(String name) { String normedName = name.toLowerCase(Locale.ROOT); EntityType type = new EntityType(normedName); EntityType canonical = TYPE_CACHE.putIfAbsent(normedName, type); return canonical == null ? type : canonical; } }
/** * {@inheritDoc} * * This implementation uses an internal {@link Query} implementation to prepare queries for * {@link #streamEntities(EntityQuery)} and {@link #streamEntityGroups(EntityQuery, TypedName)}. */ @Override public <V extends Entity> Query<V> query(Class<V> type) { EntityType etype = viewClassCache.get(type); if (etype == null) { DefaultEntityType det = type.getAnnotation(DefaultEntityType.class); if (det == null) { throw new IllegalArgumentException(type + " has no default entity type annotation"); } etype = EntityType.forName(det.value()); viewClassCache.put(type, etype); } return new JavaQuery<>(this, etype, type); }
/** * Look up the defaults for a particular entity type. * @param type The entity type. * @return The defaults. */ @Nullable public static EntityDefaults lookup(EntityType type) { // TODO Cache these defaults String name = type.getName(); String path = String.format("META-INF/lenskit/entity-defaults/%s.yaml", name); try (InputStream stream = ClassLoaders.inferDefault(EntityDefaults.class) .getResourceAsStream(path)) { if (stream == null) { return null; } ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); DefaultsBean bean = mapper.readValue(stream, DefaultsBean.class); return fromBean(type, bean); } catch (IOException e) { throw new UncheckedIOException("error defaults for " + type, e); } }
EntityType etype = EntityType.forName(eTypeName); logger.debug("{}: reading entities of type {}", name, etype); EntityDefaults entityDefaults = EntityDefaults.lookup(etype);
@Override public ObjectNode toJSON() { JsonNodeFactory nf = JsonNodeFactory.instance; ObjectNode json = nf.objectNode(); json.put("format", "json"); json.put("entity_type", entityType.getName()); if (!attributes.isEmpty()) { ObjectNode attrNode = json.putObject("attributes"); for (Map.Entry<String,TypedName<?>> attr: attributes.entrySet()) { ObjectNode an = attrNode.putObject(attr.getKey()); an.put("name", attr.getValue().getName()); an.put("type", TypeUtils.makeTypeName(attr.getValue().getType())); } } return json; }
json.put("format", "delimited"); json.put("delimiter", delimiter); json.put("entity_type", entityType.getName()); json.put("base_id", getBaseId()); if (readHeader) {
EntityType et = EntityType.forName(d.getKey()); TypedName<?> attr = attrs.get(d.getValue()); Verify.verify(attr.getType().equals(LONG_TYPE),
node.set("format", fac.textNode("tsv")); node.set("file", fac.textNode(ITEM_FILE_NAME)); node.set("entity_type", fac.textNode(CommonTypes.ITEM.getName())); ArrayNode cols = fac.arrayNode(); cols.add(CommonAttributes.ENTITY_ID.getName());
EntityType etype = EntityType.forName(eTypeName); logger.debug("{}: reading entities of type {}", name, etype); EntityDefaults entityDefaults = EntityDefaults.lookup(etype);
train.set("file", nf.textNode(outputDir.relativize(trainFiles.get(i)).toString())); train.set("format", nf.textNode("csv")); train.set("entity_type", nf.textNode(entityType.getName())); train.set("metadata", mapper.valueToTree(meta)); trainList.add(train); test.set("file", nf.textNode(outputDir.relativize(testFiles.get(i)).toString())); test.set("format", nf.textNode("csv")); test.set("entity_type", nf.textNode(entityType.getName())); test.set("metadata", mapper.valueToTree(meta)); mapper.writeValue(testManifestFiles.get(i).toFile(), test);
@Test public void testMissingDefaults() { EntityDefaults defaults = EntityDefaults.lookup(EntityType.forName("wombat")); // we don't know anything about wombats assertThat(defaults, nullValue()); }
@Nullable @Override public String apply(@Nullable EntityType input) { return input == null ? null : input.getName(); } };