2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * A value model for a {@link Property property} of a host object. This class can be used for
21 * both reflective and non-reflective property implementations.
23 * @param <H> the host type, where the host is the object that holds this property
24 * @param <T> the value type
29 public class PropertyValueModel<H, T> extends ValueModel<T> {
30 private final H mHost;
31 private final Property<H, T> mProperty;
33 private PropertyValueModel(H host, Property<H, T> property) {
48 * Returns the property.
50 * @return the property
52 public Property<H, T> getProperty() {
57 public Class<T> getType() {
58 return mProperty.getType();
63 return mProperty.get(mHost);
67 public void set(T value) {
68 mProperty.set(mHost, value);
72 * Return an appropriate PropertyValueModel for this host and property.
74 * @param host the host
75 * @param property the property
76 * @return the value model
78 public static <H, T> PropertyValueModel<H, T> of(H host, Property<H, T> property) {
79 return new PropertyValueModel<H, T>(host, property);
83 * Return a PropertyValueModel for this {@code host} and a
84 * reflective property, constructed from this {@code propertyType} and {@code propertyName}.
87 * @param propertyType the property type
88 * @param propertyName the property name
89 * @return a value model with this host and a reflective property with this type and name
93 public static <H, T> PropertyValueModel<H, T> of(H host, Class<T> propertyType,
94 String propertyName) {
95 return of(host, Property.of((Class<H>) host.getClass(), propertyType, propertyName));
98 private static Class getNullaryMethodReturnType(Class c, String name) {
100 return c.getMethod(name).getReturnType();
101 } catch (NoSuchMethodException e) {
106 private static Class getFieldType(Class c, String name) {
108 return c.getField(name).getType();
109 } catch (NoSuchFieldException e) {
114 private static String capitalize(String name) {
115 if (name.isEmpty()) {
118 return Character.toUpperCase(name.charAt(0)) + name.substring(1);
122 * Return a PropertyValueModel for this {@code host} and and {@code propertyName}.
124 * @param host the host
125 * @param propertyName the property name
126 * @return a value model with this host and a reflective property with this name
128 public static PropertyValueModel of(Object host, String propertyName) {
129 Class clazz = host.getClass();
130 String suffix = capitalize(propertyName);
131 Class propertyType = getNullaryMethodReturnType(clazz, "get" + suffix);
132 if (propertyType == null) {
133 propertyType = getNullaryMethodReturnType(clazz, "is" + suffix);
135 if (propertyType == null) {
136 propertyType = getFieldType(clazz, propertyName);
138 if (propertyType == null) {
139 throw new NoSuchPropertyException(propertyName);
141 return of(host, propertyType, propertyName);