Rewrite and reorder the methods
Project: http://git-wip-us.apache.org/repos/asf/incubator-reef/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-reef/commit/8378efb5 Tree: http://git-wip-us.apache.org/repos/asf/incubator-reef/tree/8378efb5 Diff: http://git-wip-us.apache.org/repos/asf/incubator-reef/diff/8378efb5 Branch: refs/heads/REEF-395 Commit: 8378efb5685216769f92b8a76f9a946dca66edef Parents: 848b5bb Author: Yunseong Lee <[email protected]> Authored: Sun Jun 21 16:47:46 2015 +0900 Committer: Yunseong Lee <[email protected]> Committed: Sun Jun 21 16:47:46 2015 +0900 ---------------------------------------------------------------------- .../formats/AvroClassHierarchySerializer.java | 202 +++++++++---------- 1 file changed, 99 insertions(+), 103 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/8378efb5/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/AvroClassHierarchySerializer.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/AvroClassHierarchySerializer.java b/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/AvroClassHierarchySerializer.java index e4f52cf..282314e 100644 --- a/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/AvroClassHierarchySerializer.java +++ b/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/AvroClassHierarchySerializer.java @@ -34,6 +34,53 @@ public class AvroClassHierarchySerializer implements ClassHierarchySerializer { public AvroClassHierarchySerializer() { } + private static ClassHierarchy fromAvro(final AvroNode n) { + return new AvroClassHierarchy(n); + } + + private static AvroNode toAvro(final ClassHierarchy ch) { + return newAvroNode(ch.getNamespace()); + } + + private static AvroNode newAvroNode(final Node n) { + final List<AvroNode> children = new ArrayList<>(); + for (final Node child : n.getChildren()) { + children.add(newAvroNode(child)); + } + if (n instanceof ClassNode) { + final ClassNode<?> cn = (ClassNode<?>) n; + final ConstructorDef<?>[] injectable = cn.getInjectableConstructors(); + final ConstructorDef<?>[] all = cn.getAllConstructors(); + final List<ConstructorDef<?>> others = new ArrayList<>(Arrays.asList(all)); + others.removeAll(Arrays.asList(injectable)); + + final List<AvroConstructorDef> injectableConstructors = new ArrayList<>(); + for (final ConstructorDef<?> inj : injectable) { + injectableConstructors.add(newConstructorDef(inj)); + } + final List<AvroConstructorDef> otherConstructors = new ArrayList<>(); + for (final ConstructorDef<?> other : others) { + otherConstructors.add(newConstructorDef(other)); + } + final List<String> implFullNames = new ArrayList<>(); + for (final ClassNode<?> impl : cn.getKnownImplementations()) { + implFullNames.add(impl.getFullName()); + } + return newClassNode(n.getName(), n.getFullName(), cn.isInjectionCandidate(), cn.isExternalConstructor(), + cn.isUnit(), injectableConstructors, otherConstructors, implFullNames, cn.getDefaultImplementation(), + children); + } else if (n instanceof NamedParameterNode) { + final NamedParameterNode<?> np = (NamedParameterNode<?>) n; + return newNamedParameterNode(np.getName(), np.getFullName(), np.getSimpleArgName(), np.getFullArgName(), + np.isSet(), np.isList(), np.getDocumentation(), np.getShortName(), + Arrays.asList(np.getDefaultInstanceAsStrings()), children); + } else if (n instanceof PackageNode) { + return newPackageNode(n.getName(), n.getFullName(), children); + } else { + throw new IllegalStateException("Encountered unknown type of Node: " + n); + } + } + private static AvroNode newClassNode(final String name, final String fullName, final boolean isInjectionCandidate, @@ -65,23 +112,23 @@ public class AvroClassHierarchySerializer implements ClassHierarchySerializer { final String fullArgClassName, final boolean isSet, final boolean isList, - final String documentation, // can be null - final String shortName, // can be null - final List<String> instanceDefault, // can be null + final String documentation, + final String shortName, + final List<String> instanceDefault, final List<AvroNode> children) { - final AvroNamedParameterNode.Builder namedParameterNodeBuilder = AvroNamedParameterNode.newBuilder() - .setSimpleArgClassName(simpleArgClassName) - .setFullArgClassName(fullArgClassName) - .setIsSet(isSet) - .setIsList(isList); - namedParameterNodeBuilder.setDocumentation(documentation); - namedParameterNodeBuilder.setShortName(shortName); - namedParameterNodeBuilder.setInstanceDefault(instanceDefault); return AvroNode.newBuilder() .setName(name) .setFullName(fullName) - .setNamedParameterNode(namedParameterNodeBuilder.build()) + .setNamedParameterNode(AvroNamedParameterNode.newBuilder() + .setSimpleArgClassName(simpleArgClassName) + .setFullArgClassName(fullArgClassName) + .setIsSet(isSet) + .setIsList(isList) + .setDocumentation(documentation) + .setShortName(shortName) + .setInstanceDefault(instanceDefault) + .build()) .setChildren(children).build(); } @@ -93,141 +140,77 @@ public class AvroClassHierarchySerializer implements ClassHierarchySerializer { .setName(name).setFullName(fullName).setChildren(children).build(); } - private static AvroConstructorDef newConstructorDef( - String fullClassName, List<AvroConstructorArg> args) { - return AvroConstructorDef.newBuilder() - .setFullArgClassName(fullClassName).setConstructorArg(args).build(); - } - private static AvroConstructorArg newConstructorArg(final String fullArgClassName, final String namedParameterName, final boolean isFuture) { - AvroConstructorArg.Builder builder = AvroConstructorArg.newBuilder() + return AvroConstructorArg.newBuilder() .setFullArgClassName(fullArgClassName) - .setIsInjectionFuture(isFuture); - if (namedParameterName != null) { - builder.setNamedParameterName(namedParameterName).build(); - } - return builder.build(); + .setIsInjectionFuture(isFuture) + .setNamedParameterName(namedParameterName).build(); } - private static AvroConstructorDef serializeConstructorDef(final ConstructorDef<?> def) { + private static AvroConstructorDef newConstructorDef(final ConstructorDef<?> def) { final List<AvroConstructorArg> args = new ArrayList<>(); - for (ConstructorArg arg : def.getArgs()) { + for (final ConstructorArg arg : def.getArgs()) { args.add(newConstructorArg(arg.getType(), arg.getNamedParameterName(), arg.isInjectionFuture())); } - return newConstructorDef(def.getClassName(), args); - } - - private static AvroNode toAvroNode(final Node n) { - final List<AvroNode> children = new ArrayList<>(); - for (final Node child : n.getChildren()) { - children.add(toAvroNode(child)); - } - if (n instanceof ClassNode) { - final ClassNode<?> cn = (ClassNode<?>) n; - final ConstructorDef<?>[] injectable = cn.getInjectableConstructors(); - final ConstructorDef<?>[] all = cn.getAllConstructors(); - final List<ConstructorDef<?>> others = new ArrayList<>(Arrays.asList(all)); - others.removeAll(Arrays.asList(injectable)); - - final List<AvroConstructorDef> injectableConstructors = new ArrayList<>(); - for (final ConstructorDef<?> inj : injectable) { - injectableConstructors.add(serializeConstructorDef(inj)); - } - final List<AvroConstructorDef> otherConstructors = new ArrayList<>(); - for (final ConstructorDef<?> other : others) { - otherConstructors.add(serializeConstructorDef(other)); - } - final List<String> implFullNames = new ArrayList<>(); - for (final ClassNode<?> impl : cn.getKnownImplementations()) { - implFullNames.add(impl.getFullName()); - } - return newClassNode(n.getName(), n.getFullName(), cn.isInjectionCandidate(), cn.isExternalConstructor(), - cn.isUnit(), injectableConstructors, otherConstructors, implFullNames, cn.getDefaultImplementation(), children); - } else if (n instanceof NamedParameterNode) { - final NamedParameterNode<?> np = (NamedParameterNode<?>) n; - return newNamedParameterNode(np.getName(), np.getFullName(), np.getSimpleArgName(), np.getFullArgName(), - np.isSet(), np.isList(), np.getDocumentation(), np.getShortName(), - Arrays.asList(np.getDefaultInstanceAsStrings()), children); - } else if (n instanceof PackageNode) { - return newPackageNode(n.getName(), n.getFullName(), children); - } else { - throw new IllegalStateException("Encountered unknown type of Node: " + n); - } - } - - public AvroClassHierarchy toAvro(final ClassHierarchy classHierarchy) { - return new AvroClassHierarchy(toAvroNode(classHierarchy.getNamespace())); + return AvroConstructorDef.newBuilder() + .setFullArgClassName(def.getClassName()) + .setConstructorArg(args).build(); } @Override public void toFile(final ClassHierarchy classHierarchy, final File file) throws IOException { - final AvroNode avroNode = toAvroNode(classHierarchy.getNamespace()); + final AvroNode avroNode = toAvro(classHierarchy); final DatumWriter<AvroNode> avroNodeWriter = new SpecificDatumWriter<>(AvroNode.class); - try (DataFileWriter<AvroNode> dataFileWriter = new DataFileWriter<>(avroNodeWriter)) { + try (final DataFileWriter<AvroNode> dataFileWriter = new DataFileWriter<>(avroNodeWriter)) { dataFileWriter.create(avroNode.getSchema(), file); dataFileWriter.append(avroNode); } } @Override - public void toTextFile(ClassHierarchy classHierarchy, File file) throws IOException { - try (final Writer w = new FileWriter(file)) { - w.write(this.toString(classHierarchy)); - } - } - - @Override public byte[] toByteArray(final ClassHierarchy classHierarchy) throws IOException { final DatumWriter<AvroNode> requestWriter = new SpecificDatumWriter<>(AvroNode.class); try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) { final BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(out, null); - // TODO Should be a better way - requestWriter.write(toAvroNode(classHierarchy.getNamespace()), encoder); + requestWriter.write(toAvro(classHierarchy), encoder); encoder.flush(); return out.toByteArray(); - } catch (final IOException e) { + } catch (IOException e) { throw new RuntimeException(e); } } - // TODO Improve the interface? @Override - public String toString(ClassHierarchy classHierarchy) throws IOException { + public String toString(final ClassHierarchy classHierarchy) throws IOException { final DatumWriter<AvroNode> configurationWriter = new SpecificDatumWriter<>(AvroNode.class); try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) { final JsonEncoder encoder = EncoderFactory.get().jsonEncoder(AvroConfiguration.SCHEMA$, out); - configurationWriter.write(toAvroNode(classHierarchy.getNamespace()), encoder); + configurationWriter.write(toAvro(classHierarchy), encoder); encoder.flush(); out.flush(); return out.toString(JSON_CHARSET); - } catch (final IOException e) { + } catch (IOException e) { throw new RuntimeException(e); } } @Override + public void toTextFile(ClassHierarchy classHierarchy, File file) throws IOException { + try (final Writer w = new FileWriter(file)) { + w.write(this.toString(classHierarchy)); + } + } + + @Override public ClassHierarchy fromFile(final File file) throws IOException { final AvroNode avroNode; try (final DataFileReader<AvroNode> dataFileReader = new DataFileReader<>(file, new SpecificDatumReader<>(AvroNode.class))) { avroNode = dataFileReader.next(); } - return new AvroClassHierarchy(avroNode); - } - - @Override - public ClassHierarchy fromTextFile(final File file) throws IOException { - final StringBuilder stringBuilder = new StringBuilder(); - try (final BufferedReader reader = new BufferedReader(new FileReader(file))) { - String line = reader.readLine(); - while (line != null) { - stringBuilder.append(line); - line = reader.readLine(); - } - } - return fromString(stringBuilder.toString()); + return fromAvro(avroNode); } @Override @@ -235,8 +218,8 @@ public class AvroClassHierarchySerializer implements ClassHierarchySerializer { try { final BinaryDecoder decoder = DecoderFactory.get().binaryDecoder(theBytes, null); final SpecificDatumReader<AvroNode> reader = new SpecificDatumReader<>(AvroNode.class); - return new AvroClassHierarchy(reader.read(null, decoder)); - } catch (final IOException e) { + return fromAvro(reader.read(null, decoder)); + } catch (IOException e) { throw new RuntimeException(e); } } @@ -246,9 +229,22 @@ public class AvroClassHierarchySerializer implements ClassHierarchySerializer { try { final JsonDecoder decoder = DecoderFactory.get().jsonDecoder(AvroNode.getClassSchema(), theString); final SpecificDatumReader<AvroNode> reader = new SpecificDatumReader<>(AvroNode.class); - return new AvroClassHierarchy(reader.read(null, decoder)); - } catch (final IOException e) { + return fromAvro(reader.read(null, decoder)); + } catch (IOException e) { throw new RuntimeException(e); } } + + @Override + public ClassHierarchy fromTextFile(final File file) throws IOException { + final StringBuilder stringBuilder = new StringBuilder(); + try (final BufferedReader reader = new BufferedReader(new FileReader(file))) { + String line = reader.readLine(); + while (line != null) { + stringBuilder.append(line); + line = reader.readLine(); + } + } + return fromString(stringBuilder.toString()); + } }
