|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package co.elastic.clients.util; |
| 21 | + |
| 22 | +import co.elastic.clients.json.JsonpDeserializer; |
| 23 | +import co.elastic.clients.json.JsonpDeserializerBase; |
| 24 | +import co.elastic.clients.json.JsonpMapper; |
| 25 | +import co.elastic.clients.json.JsonpMappingException; |
| 26 | +import co.elastic.clients.json.JsonpUtils; |
| 27 | +import jakarta.json.stream.JsonParser; |
| 28 | + |
| 29 | +import java.util.EnumSet; |
| 30 | +import java.util.function.Function; |
| 31 | + |
| 32 | +/** |
| 33 | + * A key/value pair. Used to represent pairs where the name is not a string (generally an enum). |
| 34 | + * <p> |
| 35 | + * The key must have a string representation in JSON. |
| 36 | + */ |
| 37 | +public class Pair<K, V> { |
| 38 | + |
| 39 | + private final K name; |
| 40 | + private final V value; |
| 41 | + |
| 42 | + public Pair(K name, V value) { |
| 43 | + this.name = name; |
| 44 | + this.value = value; |
| 45 | + } |
| 46 | + |
| 47 | + public K key() { |
| 48 | + return this.name; |
| 49 | + } |
| 50 | + |
| 51 | + public V value() { |
| 52 | + return this.value; |
| 53 | + } |
| 54 | + |
| 55 | + public static <K, V> Pair<K, V> of(K name, V value) { |
| 56 | + return new Pair<>(name, value); |
| 57 | + } |
| 58 | + |
| 59 | + public static <K, V> JsonpDeserializer<Pair<K, V>> deserializer(Function<String, K> keyDeserializer, JsonpDeserializer<V> valueDeserializer) { |
| 60 | + return new JsonpDeserializerBase<Pair<K, V>>(EnumSet.of(JsonParser.Event.START_OBJECT)) { |
| 61 | + @Override |
| 62 | + public Pair<K, V> deserialize(JsonParser parser, JsonpMapper mapper, JsonParser.Event event) { |
| 63 | + |
| 64 | + JsonpUtils.expectNextEvent(parser, JsonParser.Event.KEY_NAME); |
| 65 | + String name = parser.getString(); |
| 66 | + |
| 67 | + try { |
| 68 | + K key = keyDeserializer.apply(parser.getString()); |
| 69 | + V value = valueDeserializer.deserialize(parser, mapper); |
| 70 | + JsonpUtils.expectNextEvent(parser, JsonParser.Event.END_OBJECT); |
| 71 | + |
| 72 | + return new Pair<>(key, value); |
| 73 | + } catch (Exception e) { |
| 74 | + throw JsonpMappingException.from(e, null, name, parser); |
| 75 | + } |
| 76 | + } |
| 77 | + }; |
| 78 | + } |
| 79 | +} |
0 commit comments