Line data Source code
1 : // Copyright (C) 2021 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.index.testing; 16 : 17 : import com.google.gerrit.common.Nullable; 18 : import com.google.gerrit.index.StoredValue; 19 : import com.google.protobuf.MessageLite; 20 : import java.sql.Timestamp; 21 : 22 : /** Bridge to recover fields from the fake index. */ 23 : public class FakeStoredValue implements StoredValue { 24 : private final Object field; 25 : /** 26 : * Some index implementations store protos, some convert them to bytes first. 27 : * 28 : * <p>This property is true if field is stored in proto format. 29 : */ 30 : private final boolean isProto; 31 : 32 97 : public FakeStoredValue(Object field) { 33 97 : this.field = field; 34 97 : this.isProto = false; 35 97 : } 36 : 37 1 : public FakeStoredValue(Object field, boolean isProto) { 38 1 : this.field = field; 39 1 : this.isProto = isProto; 40 1 : } 41 : 42 : @Override 43 : public String asString() { 44 97 : return (String) field; 45 : } 46 : 47 : @Override 48 : @SuppressWarnings("unchecked") 49 : public Iterable<String> asStrings() { 50 97 : return (Iterable<String>) field; 51 : } 52 : 53 : @Override 54 : public Integer asInteger() { 55 97 : return (Integer) field; 56 : } 57 : 58 : @Override 59 : @SuppressWarnings("unchecked") 60 : public Iterable<Integer> asIntegers() { 61 97 : return (Iterable<Integer>) field; 62 : } 63 : 64 : @Override 65 : public Long asLong() { 66 1 : return (Long) field; 67 : } 68 : 69 : @Override 70 : @SuppressWarnings("unchecked") 71 : public Iterable<Long> asLongs() { 72 1 : return (Iterable<Long>) field; 73 : } 74 : 75 : @Override 76 : public Timestamp asTimestamp() { 77 97 : return (Timestamp) field; 78 : } 79 : 80 : @Override 81 : public byte[] asByteArray() { 82 97 : return (byte[]) field; 83 : } 84 : 85 : @Override 86 : @Nullable 87 : public MessageLite asProto() { 88 1 : if (isProto) { 89 1 : return (MessageLite) field; 90 : } 91 1 : return null; 92 : } 93 : 94 : @Override 95 : @Nullable 96 : @SuppressWarnings("unchecked") 97 : public Iterable<MessageLite> asProtos() { 98 1 : if (isProto) { 99 1 : return (Iterable<MessageLite>) field; 100 : } 101 1 : return null; 102 : } 103 : 104 : @Override 105 : @SuppressWarnings("unchecked") 106 : public Iterable<byte[]> asByteArrays() { 107 97 : return (Iterable<byte[]>) field; 108 : } 109 : }