1 /* 2 * Copyright (C) 2011 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 package android.util; 17 18 19 /** 20 * A property is an abstraction that can be used to represent a <emb>mutable</em> value that is held 21 * in a <em>host</em> object. The Property's {@link #set(Object, Object)} or {@link #get(Object)} 22 * methods can be implemented in terms of the private fields of the host object, or via "setter" and 23 * "getter" methods or by some other mechanism, as appropriate. 24 * 25 * @param <T> The class on which the property is declared. 26 * @param <V> The type that this property represents. 27 */ 28 @android.ravenwood.annotation.RavenwoodKeepWholeClass 29 public abstract class Property<T, V> { 30 31 private final String mName; 32 private final Class<V> mType; 33 34 /** 35 * This factory method creates and returns a Property given the <code>class</code> and 36 * <code>name</code> parameters, where the <code>"name"</code> parameter represents either: 37 * <ul> 38 * <li>a public <code>getName()</code> method on the class which takes no arguments, plus an 39 * optional public <code>setName()</code> method which takes a value of the same type 40 * returned by <code>getName()</code> 41 * <li>a public <code>isName()</code> method on the class which takes no arguments, plus an 42 * optional public <code>setName()</code> method which takes a value of the same type 43 * returned by <code>isName()</code> 44 * <li>a public <code>name</code> field on the class 45 * </ul> 46 * 47 * <p>If either of the get/is method alternatives is found on the class, but an appropriate 48 * <code>setName()</code> method is not found, the <code>Property</code> will be 49 * {@link #isReadOnly() readOnly}. Calling the {@link #set(Object, Object)} method on such 50 * a property is allowed, but will have no effect.</p> 51 * 52 * <p>If neither the methods nor the field are found on the class a 53 * {@link NoSuchPropertyException} exception will be thrown.</p> 54 */ of(Class<T> hostType, Class<V> valueType, String name)55 public static <T, V> Property<T, V> of(Class<T> hostType, Class<V> valueType, String name) { 56 return new ReflectiveProperty<T, V>(hostType, valueType, name); 57 } 58 59 /** 60 * A constructor that takes an identifying name and {@link #getType() type} for the property. 61 */ Property(Class<V> type, String name)62 public Property(Class<V> type, String name) { 63 mName = name; 64 mType = type; 65 } 66 67 /** 68 * Returns true if the {@link #set(Object, Object)} method does not set the value on the target 69 * object (in which case the {@link #set(Object, Object) set()} method should throw a {@link 70 * NoSuchPropertyException} exception). This may happen if the Property wraps functionality that 71 * allows querying the underlying value but not setting it. For example, the {@link #of(Class, 72 * Class, String)} factory method may return a Property with name "foo" for an object that has 73 * only a <code>getFoo()</code> or <code>isFoo()</code> method, but no matching 74 * <code>setFoo()</code> method. 75 */ isReadOnly()76 public boolean isReadOnly() { 77 return false; 78 } 79 80 /** 81 * Sets the value on <code>object</code> which this property represents. If the method is unable 82 * to set the value on the target object it will throw an {@link UnsupportedOperationException} 83 * exception. 84 */ set(T object, V value)85 public void set(T object, V value) { 86 throw new UnsupportedOperationException("Property " + getName() +" is read-only"); 87 } 88 89 /** 90 * Returns the current value that this property represents on the given <code>object</code>. 91 */ get(T object)92 public abstract V get(T object); 93 94 /** 95 * Returns the name for this property. 96 */ getName()97 public String getName() { 98 return mName; 99 } 100 101 /** 102 * Returns the type for this property. 103 */ getType()104 public Class<V> getType() { 105 return mType; 106 } 107 } 108