1b39f051631250c49936a475d0e64584afb7f1b93Chet Haase/* 2b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * Copyright (C) 2011 The Android Open Source Project 3b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * 4b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * Licensed under the Apache License, Version 2.0 (the "License"); 5b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * you may not use this file except in compliance with the License. 6b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * You may obtain a copy of the License at 7b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * 8b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * http://www.apache.org/licenses/LICENSE-2.0 9b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * 10b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * Unless required by applicable law or agreed to in writing, software 11b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * distributed under the License is distributed on an "AS IS" BASIS, 12b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * See the License for the specific language governing permissions and 14b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * limitations under the License. 15b39f051631250c49936a475d0e64584afb7f1b93Chet Haase */ 16b39f051631250c49936a475d0e64584afb7f1b93Chet Haasepackage android.util; 17b39f051631250c49936a475d0e64584afb7f1b93Chet Haase 18b39f051631250c49936a475d0e64584afb7f1b93Chet Haase 19b39f051631250c49936a475d0e64584afb7f1b93Chet Haase/** 20b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * A property is an abstraction that can be used to represent a <emb>mutable</em> value that is held 21b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * in a <em>host</em> object. The Property's {@link #set(Object, Object)} or {@link #get(Object)} 22b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * methods can be implemented in terms of the private fields of the host object, or via "setter" and 23b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * "getter" methods or by some other mechanism, as appropriate. 24b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * 25b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * @param <T> The class on which the property is declared. 26b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * @param <V> The type that this property represents. 27b39f051631250c49936a475d0e64584afb7f1b93Chet Haase */ 28b39f051631250c49936a475d0e64584afb7f1b93Chet Haasepublic abstract class Property<T, V> { 29b39f051631250c49936a475d0e64584afb7f1b93Chet Haase 30b39f051631250c49936a475d0e64584afb7f1b93Chet Haase private final String mName; 31b39f051631250c49936a475d0e64584afb7f1b93Chet Haase private final Class<V> mType; 32b39f051631250c49936a475d0e64584afb7f1b93Chet Haase 33b39f051631250c49936a475d0e64584afb7f1b93Chet Haase /** 34b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * This factory method creates and returns a Property given the <code>class</code> and 35b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * <code>name</code> parameters, where the <code>"name"</code> parameter represents either: 36b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * <ul> 37b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * <li>a public <code>getName()</code> method on the class which takes no arguments, plus an 38b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * optional public <code>setName()</code> method which takes a value of the same type 39b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * returned by <code>getName()</code> 40b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * <li>a public <code>isName()</code> method on the class which takes no arguments, plus an 41b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * optional public <code>setName()</code> method which takes a value of the same type 42b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * returned by <code>isName()</code> 43b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * <li>a public <code>name</code> field on the class 44b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * </ul> 45b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * 46b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * <p>If either of the get/is method alternatives is found on the class, but an appropriate 47b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * <code>setName()</code> method is not found, the <code>Property</code> will be 48b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * {@link #isReadOnly() readOnly}. Calling the {@link #set(Object, Object)} method on such 49b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * a property is allowed, but will have no effect.</p> 50b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * 51b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * <p>If neither the methods nor the field are found on the class a 52b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * {@link NoSuchPropertyException} exception will be thrown.</p> 53b39f051631250c49936a475d0e64584afb7f1b93Chet Haase */ 54b39f051631250c49936a475d0e64584afb7f1b93Chet Haase public static <T, V> Property<T, V> of(Class<T> hostType, Class<V> valueType, String name) { 55b39f051631250c49936a475d0e64584afb7f1b93Chet Haase return new ReflectiveProperty<T, V>(hostType, valueType, name); 56b39f051631250c49936a475d0e64584afb7f1b93Chet Haase } 57b39f051631250c49936a475d0e64584afb7f1b93Chet Haase 58b39f051631250c49936a475d0e64584afb7f1b93Chet Haase /** 59b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * A constructor that takes an identifying name and {@link #getType() type} for the property. 60b39f051631250c49936a475d0e64584afb7f1b93Chet Haase */ 61b39f051631250c49936a475d0e64584afb7f1b93Chet Haase public Property(Class<V> type, String name) { 62b39f051631250c49936a475d0e64584afb7f1b93Chet Haase mName = name; 63b39f051631250c49936a475d0e64584afb7f1b93Chet Haase mType = type; 64b39f051631250c49936a475d0e64584afb7f1b93Chet Haase } 65b39f051631250c49936a475d0e64584afb7f1b93Chet Haase 66b39f051631250c49936a475d0e64584afb7f1b93Chet Haase /** 67b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * Returns true if the {@link #set(Object, Object)} method does not set the value on the target 68b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * object (in which case the {@link #set(Object, Object) set()} method should throw a {@link 69b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * NoSuchPropertyException} exception). This may happen if the Property wraps functionality that 70b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * allows querying the underlying value but not setting it. For example, the {@link #of(Class, 71b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * Class, String)} factory method may return a Property with name "foo" for an object that has 72b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * only a <code>getFoo()</code> or <code>isFoo()</code> method, but no matching 73b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * <code>setFoo()</code> method. 74b39f051631250c49936a475d0e64584afb7f1b93Chet Haase */ 75b39f051631250c49936a475d0e64584afb7f1b93Chet Haase public boolean isReadOnly() { 76b39f051631250c49936a475d0e64584afb7f1b93Chet Haase return false; 77b39f051631250c49936a475d0e64584afb7f1b93Chet Haase } 78b39f051631250c49936a475d0e64584afb7f1b93Chet Haase 79b39f051631250c49936a475d0e64584afb7f1b93Chet Haase /** 80b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * Sets the value on <code>object</code> which this property represents. If the method is unable 81a2f78a3617170aadf91be568e4fbbe56f9dbeebfChet Haase * to set the value on the target object it will throw an {@link UnsupportedOperationException} 82b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * exception. 83b39f051631250c49936a475d0e64584afb7f1b93Chet Haase */ 84b39f051631250c49936a475d0e64584afb7f1b93Chet Haase public void set(T object, V value) { 85a2f78a3617170aadf91be568e4fbbe56f9dbeebfChet Haase throw new UnsupportedOperationException("Property " + getName() +" is read-only"); 86b39f051631250c49936a475d0e64584afb7f1b93Chet Haase } 87b39f051631250c49936a475d0e64584afb7f1b93Chet Haase 88b39f051631250c49936a475d0e64584afb7f1b93Chet Haase /** 89b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * Returns the current value that this property represents on the given <code>object</code>. 90b39f051631250c49936a475d0e64584afb7f1b93Chet Haase */ 91b39f051631250c49936a475d0e64584afb7f1b93Chet Haase public abstract V get(T object); 92b39f051631250c49936a475d0e64584afb7f1b93Chet Haase 93b39f051631250c49936a475d0e64584afb7f1b93Chet Haase /** 94b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * Returns the name for this property. 95b39f051631250c49936a475d0e64584afb7f1b93Chet Haase */ 96b39f051631250c49936a475d0e64584afb7f1b93Chet Haase public String getName() { 97b39f051631250c49936a475d0e64584afb7f1b93Chet Haase return mName; 98b39f051631250c49936a475d0e64584afb7f1b93Chet Haase } 99b39f051631250c49936a475d0e64584afb7f1b93Chet Haase 100b39f051631250c49936a475d0e64584afb7f1b93Chet Haase /** 101b39f051631250c49936a475d0e64584afb7f1b93Chet Haase * Returns the type for this property. 102b39f051631250c49936a475d0e64584afb7f1b93Chet Haase */ 103b39f051631250c49936a475d0e64584afb7f1b93Chet Haase public Class<V> getType() { 104b39f051631250c49936a475d0e64584afb7f1b93Chet Haase return mType; 105b39f051631250c49936a475d0e64584afb7f1b93Chet Haase } 106b39f051631250c49936a475d0e64584afb7f1b93Chet Haase} 107