beam-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [beam] reuvenlax commented on a change in pull request #10413: [BEAM-9035] Typed options for Row Schema and Field
Date Wed, 11 Mar 2020 03:18:37 GMT
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_r390710604
 
 

 ##########
 File path: sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/Schema.java
 ##########
 @@ -953,6 +1012,338 @@ 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 setByteOption(String optionName, Byte value) {
+        setOption(optionName, FieldType.BYTE, value);
+        return this;
+      }
+
+      public Builder setBytesOption(String optionName, byte[] value) {
+        setOption(optionName, FieldType.BYTES, value);
+        return this;
+      }
+
+      public Builder setInt16Option(String optionName, Short value) {
+        setOption(optionName, FieldType.INT16, value);
+        return this;
+      }
+
+      public Builder setInt32Option(String optionName, Integer value) {
+        setOption(optionName, FieldType.INT32, value);
+        return this;
+      }
+
+      public Builder setInt64Option(String optionName, Long value) {
+        setOption(optionName, FieldType.INT64, value);
+        return this;
+      }
+
+      public Builder setDecimalOption(String optionName, BigDecimal value) {
+        setOption(optionName, FieldType.DECIMAL, value);
+        return this;
+      }
+
+      public Builder setFloatOption(String optionName, Float value) {
+        setOption(optionName, FieldType.FLOAT, value);
+        return this;
+      }
+
+      public Builder setDoubleOption(String optionName, Double value) {
+        setOption(optionName, FieldType.DOUBLE, value);
+        return this;
+      }
+
+      public Builder setStringOption(String optionName, String value) {
+        setOption(optionName, FieldType.STRING, value);
+        return this;
+      }
+
+      public Builder setDateTimeOption(String optionName, ReadableDateTime value) {
+        setOption(optionName, FieldType.DATETIME, value);
+        return this;
+      }
+
+      public Builder setBooleanOption(String optionName, Boolean value) {
+        setOption(optionName, FieldType.BOOLEAN, value);
+        return this;
+      }
+
+      public <V> Builder setArrayOption(String optionName, FieldType arrayType, List<V>
value) {
+        setOption(optionName, arrayType, value);
+        return this;
+      }
+
+      public <K, V> Builder setMapOption(String optionName, FieldType mapType, Map<K,
V> value) {
+        setOption(optionName, mapType, value);
+        return this;
+      }
+
+      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) {
 
 Review comment:
   Simpler to use Preconditions.checkNotNull

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message