@Test public void testWriteCDL() { Dimension low = new Dimension("low", 1); Dimension mid = new Dimension("mid", 1); Dimension high = new Dimension("high", 1); Group.Builder parentg = Group.builder().setName("parent").addDimension(low); Group.Builder grampsb = Group.builder().setName("gramps").addGroup(parentg).addDimension(mid); Group.Builder uncleb = Group.builder().setName("uncle"); Variable.Builder<?> vb = Variable.builder().setName("v").setDataType(DataType.STRING).setParentGroupBuilder(parentg) .setDimensionsByName("low"); Variable.Builder<?> vattb = Variable.builder().setName("vatt").setDataType(DataType.STRING) .setParentGroupBuilder(parentg).setDimensionsByName("mid").addAttribute(new Attribute("findme", "findmevalue")); parentg.addVariable(vb).addAttribute(new Attribute("groupAtt", "groupVal")); grampsb.addVariable(vattb); Map<Integer, String> map = ImmutableMap.of(1, "name1", 2, "name2", 3, "name3"); EnumTypedef typedef1 = new EnumTypedef("low", map); parentg.addEnumTypedef(typedef1); Group root = Group.builder().addGroup(grampsb).addGroup(uncleb).addDimension(high).build(); Group parent = root.findGroupNested("parent").orElse(null); assertThat(parent).isNotNull(); Group gramps = root.findGroupNested("gramps").orElse(null); assertThat(gramps).isNotNull(); Group uncle = root.findGroupNested("uncle").orElse(null); assertThat(uncle).isNotNull(); assertThat(gramps.toString()).startsWith(String.format("dimensions:%n" + " mid = 1;%n" + "variables:%n" + " String vatt(mid=1);%n" + " :findme = \"findmevalue\";%n" + "%n" + "group: parent {%n" + " types:%n" + " enum low { 'name1' = 1, 'name2' = 2, 'name3' = 3};%n" + "%n" + " dimensions:%n" + " low = 1;%n" + " variables:%n" + " String v(low=1);%n" + "%n" + " + " :groupAtt = \"groupVal\";%n" + "}")); assertThat(uncle.toString()).isEqualTo(""); }
void writeCDL(Formatter out, Indent indent, boolean strict) { boolean hasE = (!enumTypedefs.isEmpty()); boolean hasD = (!dimensions.isEmpty()); boolean hasV = (!variables.isEmpty()); boolean hasA = (!Iterables.isEmpty(attributes)); if (hasE) { out.format("%stypes:%n", indent); indent.incr(); for (EnumTypedef e : enumTypedefs) { e.writeCDL(out, indent, strict); out.format("%n"); } indent.decr(); out.format("%n"); } if (hasD) { out.format("%sdimensions:%n", indent); indent.incr(); for (Dimension myd : dimensions) { myd.writeCDL(out, indent, strict); out.format("%n"); } indent.decr(); } if (hasV) { out.format("%svariables:%n", indent); indent.incr(); for (Variable v : variables) { v.writeCDL(out, indent, false, strict); out.format("%n"); } indent.decr(); } for (Group g : groups) { String gname = strict ? NetcdfFiles.makeValidCDLName(g.getShortName()) : g.getShortName(); out.format("%sgroup: %s {%n", indent, gname); indent.incr(); g.writeCDL(out, indent, strict); indent.decr(); out.format("%s}%n%n", indent); } if (hasA) { if (isRoot()) out.format("%s else out.format("%s for (Attribute att : attributes) { out.format("%s", indent); att.writeCDL(out, strict, null); out.format(";"); if (!strict && (att.getDataType() != DataType.STRING)) out.format(" out.format("%n"); } } }
Group { void writeCDL(Formatter out, Indent indent, boolean strict) { boolean hasE = (!enumTypedefs.isEmpty()); boolean hasD = (!dimensions.isEmpty()); boolean hasV = (!variables.isEmpty()); boolean hasA = (!Iterables.isEmpty(attributes)); if (hasE) { out.format("%stypes:%n", indent); indent.incr(); for (EnumTypedef e : enumTypedefs) { e.writeCDL(out, indent, strict); out.format("%n"); } indent.decr(); out.format("%n"); } if (hasD) { out.format("%sdimensions:%n", indent); indent.incr(); for (Dimension myd : dimensions) { myd.writeCDL(out, indent, strict); out.format("%n"); } indent.decr(); } if (hasV) { out.format("%svariables:%n", indent); indent.incr(); for (Variable v : variables) { v.writeCDL(out, indent, false, strict); out.format("%n"); } indent.decr(); } for (Group g : groups) { String gname = strict ? NetcdfFiles.makeValidCDLName(g.getShortName()) : g.getShortName(); out.format("%sgroup: %s {%n", indent, gname); indent.incr(); g.writeCDL(out, indent, strict); indent.decr(); out.format("%s}%n%n", indent); } if (hasA) { if (isRoot()) out.format("%s else out.format("%s for (Attribute att : attributes) { out.format("%s", indent); att.writeCDL(out, strict, null); out.format(";"); if (!strict && (att.getDataType() != DataType.STRING)) out.format(" out.format("%n"); } } } }
Group { void writeCDL(Formatter out, Indent indent, boolean strict) { boolean hasE = (!enumTypedefs.isEmpty()); boolean hasD = (!dimensions.isEmpty()); boolean hasV = (!variables.isEmpty()); boolean hasA = (!Iterables.isEmpty(attributes)); if (hasE) { out.format("%stypes:%n", indent); indent.incr(); for (EnumTypedef e : enumTypedefs) { e.writeCDL(out, indent, strict); out.format("%n"); } indent.decr(); out.format("%n"); } if (hasD) { out.format("%sdimensions:%n", indent); indent.incr(); for (Dimension myd : dimensions) { myd.writeCDL(out, indent, strict); out.format("%n"); } indent.decr(); } if (hasV) { out.format("%svariables:%n", indent); indent.incr(); for (Variable v : variables) { v.writeCDL(out, indent, false, strict); out.format("%n"); } indent.decr(); } for (Group g : groups) { String gname = strict ? NetcdfFiles.makeValidCDLName(g.getShortName()) : g.getShortName(); out.format("%sgroup: %s {%n", indent, gname); indent.incr(); g.writeCDL(out, indent, strict); indent.decr(); out.format("%s}%n%n", indent); } if (hasA) { if (isRoot()) out.format("%s else out.format("%s for (Attribute att : attributes) { out.format("%s", indent); att.writeCDL(out, strict, null); out.format(";"); if (!strict && (att.getDataType() != DataType.STRING)) out.format(" out.format("%n"); } } } private  Group(Builder builder, @Nullable Group parent); }
Group { void writeCDL(Formatter out, Indent indent, boolean strict) { boolean hasE = (!enumTypedefs.isEmpty()); boolean hasD = (!dimensions.isEmpty()); boolean hasV = (!variables.isEmpty()); boolean hasA = (!Iterables.isEmpty(attributes)); if (hasE) { out.format("%stypes:%n", indent); indent.incr(); for (EnumTypedef e : enumTypedefs) { e.writeCDL(out, indent, strict); out.format("%n"); } indent.decr(); out.format("%n"); } if (hasD) { out.format("%sdimensions:%n", indent); indent.incr(); for (Dimension myd : dimensions) { myd.writeCDL(out, indent, strict); out.format("%n"); } indent.decr(); } if (hasV) { out.format("%svariables:%n", indent); indent.incr(); for (Variable v : variables) { v.writeCDL(out, indent, false, strict); out.format("%n"); } indent.decr(); } for (Group g : groups) { String gname = strict ? NetcdfFiles.makeValidCDLName(g.getShortName()) : g.getShortName(); out.format("%sgroup: %s {%n", indent, gname); indent.incr(); g.writeCDL(out, indent, strict); indent.decr(); out.format("%s}%n%n", indent); } if (hasA) { if (isRoot()) out.format("%s else out.format("%s for (Attribute att : attributes) { out.format("%s", indent); att.writeCDL(out, strict, null); out.format(";"); if (!strict && (att.getDataType() != DataType.STRING)) out.format(" out.format("%n"); } } } private  Group(Builder builder, @Nullable Group parent); AttributeContainer attributes(); Group commonParent(Group other); @Nullable Attribute findAttribute(String name); String findAttributeString(String attName, String defaultValue); Optional<Dimension> findDimension(String name); @Nullable Dimension findDimension(Dimension dim); @Nullable Dimension findDimensionLocal(String shortName); @Nullable EnumTypedef findEnumeration(String name); @Nullable Group findGroupLocal(String groupShortName); Optional<Group> findGroupNested(String groupShortName); @Nullable Variable findVariableByAttribute(String attName, String attValue); @Nullable Variable findVariableLocal(String varShortName); @Nullable Variable findVariableOrInParent(String varShortName); ImmutableList<Dimension> getDimensions(); ImmutableList<EnumTypedef> getEnumTypedefs(); String getFullName(); ImmutableList<Group> getGroups(); NetcdfFile getNetcdfFile(); @Nullable Group getParentGroup(); String getShortName(); ImmutableList<Variable> getVariables(); boolean isParent(Group other); boolean isRoot(); ImmutableList<Dimension> makeDimensionsList(String dimString); String getNameAndAttributes(); @Override String toString(); @Override boolean equals(Object oo); @Override int hashCode(); Builder toBuilder(); static Builder builder(); }
Group { void writeCDL(Formatter out, Indent indent, boolean strict) { boolean hasE = (!enumTypedefs.isEmpty()); boolean hasD = (!dimensions.isEmpty()); boolean hasV = (!variables.isEmpty()); boolean hasA = (!Iterables.isEmpty(attributes)); if (hasE) { out.format("%stypes:%n", indent); indent.incr(); for (EnumTypedef e : enumTypedefs) { e.writeCDL(out, indent, strict); out.format("%n"); } indent.decr(); out.format("%n"); } if (hasD) { out.format("%sdimensions:%n", indent); indent.incr(); for (Dimension myd : dimensions) { myd.writeCDL(out, indent, strict); out.format("%n"); } indent.decr(); } if (hasV) { out.format("%svariables:%n", indent); indent.incr(); for (Variable v : variables) { v.writeCDL(out, indent, false, strict); out.format("%n"); } indent.decr(); } for (Group g : groups) { String gname = strict ? NetcdfFiles.makeValidCDLName(g.getShortName()) : g.getShortName(); out.format("%sgroup: %s {%n", indent, gname); indent.incr(); g.writeCDL(out, indent, strict); indent.decr(); out.format("%s}%n%n", indent); } if (hasA) { if (isRoot()) out.format("%s else out.format("%s for (Attribute att : attributes) { out.format("%s", indent); att.writeCDL(out, strict, null); out.format(";"); if (!strict && (att.getDataType() != DataType.STRING)) out.format(" out.format("%n"); } } } private  Group(Builder builder, @Nullable Group parent); AttributeContainer attributes(); Group commonParent(Group other); @Nullable Attribute findAttribute(String name); String findAttributeString(String attName, String defaultValue); Optional<Dimension> findDimension(String name); @Nullable Dimension findDimension(Dimension dim); @Nullable Dimension findDimensionLocal(String shortName); @Nullable EnumTypedef findEnumeration(String name); @Nullable Group findGroupLocal(String groupShortName); Optional<Group> findGroupNested(String groupShortName); @Nullable Variable findVariableByAttribute(String attName, String attValue); @Nullable Variable findVariableLocal(String varShortName); @Nullable Variable findVariableOrInParent(String varShortName); ImmutableList<Dimension> getDimensions(); ImmutableList<EnumTypedef> getEnumTypedefs(); String getFullName(); ImmutableList<Group> getGroups(); NetcdfFile getNetcdfFile(); @Nullable Group getParentGroup(); String getShortName(); ImmutableList<Variable> getVariables(); boolean isParent(Group other); boolean isRoot(); ImmutableList<Dimension> makeDimensionsList(String dimString); String getNameAndAttributes(); @Override String toString(); @Override boolean equals(Object oo); @Override int hashCode(); Builder toBuilder(); static Builder builder();  }