1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.timezone.location.storage.table.reader; 18 19 /** 20 * A type for tables where the value can be expressed as a Java long or perhaps smaller. 21 */ 22 public interface LongValueTable extends Table<LongValueTable.TableEntry> { 23 24 /** 25 * Finds an entry using the supplied matcher via a binary search. If multiple entries match, an 26 * arbitrary matching entry is returned. Returns {@code null} if no entries match. 27 */ findEntry(LongValueEntryMatcher matcher)28 TableEntry findEntry(LongValueEntryMatcher matcher); 29 30 /** 31 * A type for entries in {@link LongValueTable}. 32 */ 33 interface TableEntry extends Table.TableEntry<TableEntry> { 34 35 /** 36 * Returns the entry's value. 37 */ getValue()38 long getValue(); 39 40 /** 41 * Finds an entry using the supplied matcher via a binary search. Returns {@code null} if no 42 * entry matches. Like {@link LongValueTable#findEntry(LongValueEntryMatcher)} but uses this 43 * entry as a starting point. 44 */ findNearbyEntry(LongValueEntryMatcher entryMatcher)45 TableEntry findNearbyEntry(LongValueEntryMatcher entryMatcher); 46 47 /** A helper method for implementing {@link TableEntry#equals(Object)}. */ equal(TableEntry one, Object two)48 static boolean equal(TableEntry one, Object two) { 49 if (one == null && two == null) { 50 return true; 51 } 52 if (!(two instanceof TableEntry) || one == null || one.getClass() != two.getClass()) { 53 return false; 54 } 55 TableEntry other = (TableEntry) two; 56 return (one.getKey() == other.getKey() && one.getValue() == other.getValue()); 57 } 58 59 /** A helper method for implementing {@link TableEntry#toString()}. */ toString(TableEntry entry)60 static String toString(TableEntry entry) { 61 return "TableEntry{key=" + entry.getKey() + ", value=" + entry.getValue() + "}"; 62 } 63 } 64 65 /** 66 * A matcher that can be used with {@link LongValueTable}s to find entries in the table. 67 */ 68 interface LongValueEntryMatcher { 69 70 /** 71 * Returns < 0 if the entry sought is lower than one with the values provided, > 0 72 * if the entry sought is higher than one with the values provided, and exactly zero if it 73 * is the entry sought. 74 */ compare(int key, long value)75 int compare(int key, long value); 76 } 77 } 78