OSDN Git Service

am c660c608: Merge "add Toshiba usb driver link delete Fujitsu-Toshiba bug: 9755017...
[android-x86/frameworks-base.git] / core / java / android / util / PropertyValueModel.java
1 /*
2  * Copyright (C) 2012 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 android.util;
18
19 /**
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.
22  *
23  * @param <H> the host type, where the host is the object that holds this property
24  * @param <T> the value type
25  *
26  * @see Property
27  * @see ValueModel
28  */
29 public class PropertyValueModel<H, T> extends ValueModel<T> {
30     private final H mHost;
31     private final Property<H, T> mProperty;
32
33     private PropertyValueModel(H host, Property<H, T> property) {
34         mProperty = property;
35         mHost = host;
36     }
37
38     /**
39      * Returns the host.
40      *
41      * @return the host
42      */
43     public H getHost() {
44         return mHost;
45     }
46
47     /**
48      * Returns the property.
49      *
50      * @return the property
51      */
52     public Property<H, T> getProperty() {
53         return mProperty;
54     }
55
56     @Override
57     public Class<T> getType() {
58         return mProperty.getType();
59     }
60
61     @Override
62     public T get() {
63         return mProperty.get(mHost);
64     }
65
66     @Override
67     public void set(T value) {
68         mProperty.set(mHost, value);
69     }
70
71     /**
72      * Return an appropriate PropertyValueModel for this host and property.
73      *
74      * @param host the host
75      * @param property the property
76      * @return the value model
77      */
78     public static <H, T> PropertyValueModel<H, T> of(H host, Property<H, T> property) {
79         return new PropertyValueModel<H, T>(host, property);
80     }
81
82     /**
83      * Return a PropertyValueModel for this {@code host} and a
84      * reflective property, constructed from this {@code propertyType} and {@code propertyName}.
85      *
86      * @param host
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
90      *
91      * @see Property#of
92      */
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));
96     }
97
98     private static Class getNullaryMethodReturnType(Class c, String name) {
99         try {
100             return c.getMethod(name).getReturnType();
101         } catch (NoSuchMethodException e) {
102             return null;
103         }
104     }
105
106     private static Class getFieldType(Class c, String name) {
107         try {
108             return c.getField(name).getType();
109         } catch (NoSuchFieldException e) {
110             return null;
111         }
112     }
113
114     private static String capitalize(String name) {
115         if (name.isEmpty()) {
116             return name;
117         }
118         return Character.toUpperCase(name.charAt(0)) + name.substring(1);
119     }
120
121     /**
122      * Return a PropertyValueModel for this {@code host} and and {@code propertyName}.
123      *
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
127      */
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);
134         } 
135         if (propertyType == null) {
136             propertyType = getFieldType(clazz, propertyName); 
137         }         
138         if (propertyType == null) {
139             throw new NoSuchPropertyException(propertyName); 
140         }
141         return of(host, propertyType, propertyName);
142     }
143 }