|
| 1 | +/* |
| 2 | + * Copyright (C) 2022 Vaticle |
| 3 | + * |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 5 | + * or more contributor license agreements. See the NOTICE file |
| 6 | + * distributed with this work for additional information |
| 7 | + * regarding copyright ownership. The ASF licenses this file |
| 8 | + * to you under the Apache License, Version 2.0 (the |
| 9 | + * "License"); you may not use this file except in compliance |
| 10 | + * with the License. You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, |
| 15 | + * software distributed under the License is distributed on an |
| 16 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 | + * KIND, either express or implied. See the License for the |
| 18 | + * specific language governing permissions and limitations |
| 19 | + * under the License. |
| 20 | + */ |
| 21 | + |
| 22 | +package com.vaticle.typedb.client.api.concept.value; |
| 23 | + |
| 24 | +import com.eclipsesource.json.Json; |
| 25 | +import com.eclipsesource.json.JsonObject; |
| 26 | +import com.eclipsesource.json.JsonValue; |
| 27 | +import com.vaticle.typedb.client.api.concept.Concept; |
| 28 | +import com.vaticle.typedb.client.api.concept.type.Type; |
| 29 | +import com.vaticle.typedb.client.common.exception.TypeDBClientException; |
| 30 | + |
| 31 | +import javax.annotation.CheckReturnValue; |
| 32 | + |
| 33 | +import static com.vaticle.typedb.client.api.concept.thing.Attribute.ISO_LOCAL_DATE_TIME_MILLIS; |
| 34 | +import static com.vaticle.typedb.client.common.exception.ErrorMessage.Internal.ILLEGAL_STATE; |
| 35 | + |
| 36 | +public interface Value<VALUE> extends Concept { |
| 37 | + |
| 38 | + @CheckReturnValue |
| 39 | + Type.ValueType getValueType(); |
| 40 | + |
| 41 | + @CheckReturnValue |
| 42 | + VALUE getValue(); |
| 43 | + |
| 44 | + @Override |
| 45 | + @CheckReturnValue |
| 46 | + default boolean isValue() { |
| 47 | + return true; |
| 48 | + } |
| 49 | + |
| 50 | + @CheckReturnValue |
| 51 | + default boolean isBoolean() { |
| 52 | + return false; |
| 53 | + } |
| 54 | + |
| 55 | + @CheckReturnValue |
| 56 | + default boolean isLong() { |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + @CheckReturnValue |
| 61 | + default boolean isDouble() { |
| 62 | + return false; |
| 63 | + } |
| 64 | + |
| 65 | + @CheckReturnValue |
| 66 | + default boolean isString() { |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + @CheckReturnValue |
| 71 | + default boolean isDateTime() { |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + @CheckReturnValue |
| 76 | + Value<java.lang.Boolean> asBoolean(); |
| 77 | + |
| 78 | + @CheckReturnValue |
| 79 | + Value<java.lang.Long> asLong(); |
| 80 | + |
| 81 | + @CheckReturnValue |
| 82 | + Value<java.lang.Double> asDouble(); |
| 83 | + |
| 84 | + @CheckReturnValue |
| 85 | + Value<java.lang.String> asString(); |
| 86 | + |
| 87 | + @CheckReturnValue |
| 88 | + Value<java.time.LocalDateTime> asDateTime(); |
| 89 | + |
| 90 | + @Override |
| 91 | + default JsonObject toJSON() { |
| 92 | + JsonValue value; |
| 93 | + switch (getValueType()) { |
| 94 | + case BOOLEAN: |
| 95 | + value = Json.value(asBoolean().getValue()); |
| 96 | + break; |
| 97 | + case LONG: |
| 98 | + value = Json.value(asLong().getValue()); |
| 99 | + break; |
| 100 | + case DOUBLE: |
| 101 | + value = Json.value(asDouble().getValue()); |
| 102 | + break; |
| 103 | + case STRING: |
| 104 | + value = Json.value(asString().getValue()); |
| 105 | + break; |
| 106 | + case DATETIME: |
| 107 | + value = Json.value(asDateTime().getValue().format(ISO_LOCAL_DATE_TIME_MILLIS)); |
| 108 | + break; |
| 109 | + default: |
| 110 | + throw new TypeDBClientException(ILLEGAL_STATE); |
| 111 | + } |
| 112 | + return Json.object() |
| 113 | + .add("value_type", getValueType().name().toLowerCase()) |
| 114 | + .add("value", value); |
| 115 | + } |
| 116 | + |
| 117 | + interface Boolean extends Value<java.lang.Boolean> { |
| 118 | + @Override |
| 119 | + @CheckReturnValue |
| 120 | + default boolean isBoolean() { |
| 121 | + return true; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + interface Long extends Value<java.lang.Long> { |
| 126 | + @Override |
| 127 | + @CheckReturnValue |
| 128 | + default boolean isLong() { |
| 129 | + return true; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + interface Double extends Value<java.lang.Double> { |
| 134 | + @Override |
| 135 | + @CheckReturnValue |
| 136 | + default boolean isDouble() { |
| 137 | + return true; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + interface String extends Value<java.lang.String> { |
| 142 | + @Override |
| 143 | + @CheckReturnValue |
| 144 | + default boolean isString() { |
| 145 | + return true; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + interface DateTime extends Value<java.time.LocalDateTime> { |
| 150 | + @Override |
| 151 | + @CheckReturnValue |
| 152 | + default boolean isDateTime() { |
| 153 | + return true; |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments