reuvenlax commented on a change in pull request #10413: [BEAM-9035] Typed 
options for Row Schema and Field
URL: https://github.com/apache/beam/pull/10413#discussion_r390725248
 
 

 ##########
 File path: sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/Schema.java
 ##########
 @@ -953,6 +1010,224 @@ public int hashCode() {
     }
   }
 
+  public static class Options implements Serializable {
+    private Map<String, Option> options;
+
+    @Override
+    public String toString() {
+      TreeMap sorted = new TreeMap(options);
+      return "{" + sorted + '}';
+    }
+
+    Map<String, Option> getAllOptions() {
+      return options;
+    }
+
+    public Set<String> getOptionNames() {
+      return options.keySet();
+    }
+
+    public boolean hasOptions() {
+      return options.size() > 0;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+      if (this == o) {
+        return true;
+      }
+      if (o == null || getClass() != o.getClass()) {
+        return false;
+      }
+      Options options1 = (Options) o;
+      if (!options.keySet().equals(options1.options.keySet())) {
+        return false;
+      }
+      for (Map.Entry<String, Option> optionEntry : options.entrySet()) {
+        Option thisOption = optionEntry.getValue();
+        Option otherOption = options1.options.get(optionEntry.getKey());
+        if (!thisOption.getType().equals(otherOption.getType())) {
+          return false;
+        }
+        switch (thisOption.getType().getTypeName()) {
+          case BYTE:
+          case INT16:
+          case INT32:
+          case INT64:
+          case DECIMAL:
+          case FLOAT:
+          case DOUBLE:
+          case STRING:
+          case DATETIME:
+          case BOOLEAN:
+          case ARRAY:
+          case ITERABLE:
+          case MAP:
+          case ROW:
+          case LOGICAL_TYPE:
+            if (!thisOption.getValue().equals(otherOption.getValue())) {
+              return false;
+            }
+            break;
+          case BYTES:
+            if (!Arrays.equals((byte[]) thisOption.getValue(), 
otherOption.getValue())) {
+              return false;
+            }
+        }
+      }
+      return true;
+    }
+
+    @Override
+    public int hashCode() {
+      return Objects.hash(options);
+    }
+
+    static class Option implements Serializable {
+      Option(FieldType type, Object value) {
+        this.type = type;
+        this.value = value;
+      }
+
+      private FieldType type;
+      private Object value;
+
+      @SuppressWarnings("TypeParameterUnusedInFormals")
+      <T> T getValue() {
+        return (T) value;
+      }
+
+      FieldType getType() {
+        return type;
+      }
+
+      @Override
+      public String toString() {
+        return "Option{type=" + type + ", value=" + value + '}';
+      }
+
+      @Override
+      public boolean equals(Object o) {
+        if (this == o) {
+          return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+          return false;
+        }
+        Option option = (Option) o;
+        return Objects.equals(type, option.type) && Objects.equals(value, 
option.value);
+      }
+
+      @Override
+      public int hashCode() {
+        return Objects.hash(type, value);
+      }
+    }
+
+    public static class Builder {
+      private Map<String, Option> options;
+
+      Builder(Map<String, Option> init) {
+        this.options = new HashMap<>(init);
+      }
+
+      Builder() {
+        this(new HashMap<>());
+      }
+
+      public Builder setRowOption(String optionName, Row value) {
+        setOption(optionName, FieldType.row(value.getSchema()), value);
+        return this;
+      }
+
+      public Builder setOption(String optionName, FieldType fieldType, Object 
value) {
+        if (value == null) {
+          throw new IllegalArgumentException(
+              String.format("Option %s can not be null", optionName));
+        } else {
+          options.put(
+              optionName, new Option(fieldType, verifyFieldValue(value, 
fieldType, optionName)));
+        }
+        return this;
+      }
+
+      public Builder removeOption(String optionName) {
+        options.remove(optionName);
+        return this;
+      }
+
+      public Options build() {
+        return new Options(this.options);
+      }
+
+      public Builder addOptions(Options options) {
+        this.options.putAll(options.options);
+        return this;
+      }
+    }
+
+    Options(Map<String, Option> options) {
+      this.options = options;
+    }
+
+    Options() {
+      this.options = new HashMap<>();
+    }
+
+    Options.Builder toBuilder() {
+      return new Builder(this.options);
+    }
+
+    public static Options.Builder builder() {
+      return new Builder();
+    }
+
+    public static Options none() {
+      return new Options();
+    }
+
+    @SuppressWarnings("TypeParameterUnusedInFormals")
+    @Nullable
+    public <T> T getValue(String optionName) {
+      Option option = options.get(optionName);
+      if (option != null) {
 
 Review comment:
   Maybe best to throw exception  if optionName isn't found? That's what 
Row.getValue does.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to