Line data Source code
1 : // Copyright (C) 2011 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.server.rules; 16 : 17 : import com.googlecode.prolog_cafe.exceptions.SystemException; 18 : import com.googlecode.prolog_cafe.lang.Prolog; 19 : 20 : /** 21 : * Defines a value cached in a {@link PrologEnvironment}. 22 : * 23 : * @see StoredValues 24 : */ 25 : public class StoredValue<T> { 26 : /** Construct a new unique key that does not match any other key. */ 27 : public static <T> StoredValue<T> create() { 28 0 : return new StoredValue<>(); 29 : } 30 : 31 : /** Construct a key based on a Java Class object, useful for singletons. */ 32 : public static <T> StoredValue<T> create(Class<T> clazz) { 33 104 : return new StoredValue<>(clazz); 34 : } 35 : 36 : private final Object key; 37 : 38 : /** 39 : * Initialize a stored value key using any Java Object. 40 : * 41 : * @param key unique identity of the stored value. This will be the hash key in the Prolog 42 : * Environments's hash map. 43 : */ 44 104 : public StoredValue(Object key) { 45 104 : this.key = key; 46 104 : } 47 : 48 : /** Initializes a stored value key with a new unique key. */ 49 104 : public StoredValue() { 50 104 : key = this; 51 104 : } 52 : 53 : /** Look up the value in the engine, or return null. */ 54 : public T getOrNull(Prolog engine) { 55 104 : return get((PrologEnvironment) engine.control); 56 : } 57 : /** Get the value from the engine, or throw SystemException. */ 58 : public T get(Prolog engine) { 59 104 : T obj = getOrNull(engine); 60 104 : if (obj == null) { 61 : // unless createValue() is overridden, will return null 62 4 : obj = createValue(engine); 63 4 : if (obj == null) { 64 0 : throw new SystemException("No " + key + " available"); 65 : } 66 4 : set(engine, obj); 67 : } 68 104 : return obj; 69 : } 70 : 71 : public void set(Prolog engine, T obj) { 72 4 : set((PrologEnvironment) engine.control, obj); 73 4 : } 74 : 75 : /** Perform {@link #getOrNull(Prolog)} on the environment's interpreter. */ 76 : public T get(PrologEnvironment env) { 77 104 : return env.get(this); 78 : } 79 : 80 : /** Set the value into the environment's interpreter. */ 81 : public void set(PrologEnvironment env, T obj) { 82 4 : env.set(this, obj); 83 4 : } 84 : 85 : /** 86 : * Creates a value to store, returns null by default. 87 : * 88 : * @param engine Prolog engine. 89 : * @return new value. 90 : */ 91 : protected T createValue(Prolog engine) { 92 0 : return null; 93 : } 94 : }