DefaultGriffonModelClass.java
01 /*
02  * Copyright 2008-2014 the original author or authors.
03  *
04  * Licensed under the Apache License, Version 2.0 (the "License");
05  * you may not use this file except in compliance with the License.
06  * You may obtain a copy of the License at
07  *
08  *     http://www.apache.org/licenses/LICENSE-2.0
09  *
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 org.codehaus.griffon.runtime.core.artifact;
17 
18 import griffon.core.GriffonApplication;
19 import griffon.core.artifact.GriffonModel;
20 import griffon.core.artifact.GriffonModelClass;
21 import griffon.util.GriffonClassUtils;
22 
23 import javax.annotation.Nonnull;
24 import javax.annotation.Nullable;
25 import java.util.Arrays;
26 import java.util.LinkedHashSet;
27 import java.util.Set;
28 import java.util.TreeSet;
29 
30 import static griffon.util.GriffonClassUtils.isEventHandler;
31 import static griffon.util.GriffonNameUtils.requireNonBlank;
32 import static java.util.Objects.requireNonNull;
33 
34 /**
35  @author Andres Almiray
36  @since 2.0.0
37  */
38 public class DefaultGriffonModelClass extends DefaultGriffonClass implements GriffonModelClass {
39     private static final String ERROR_MODEL_NULL = "Argument 'model' must not be null";
40     private static final String ERROR_PROPERTY_NAME_BLANK = "Argument 'propertyName' must not be blank";
41 
42     protected final Set<String> propertiesCache = new TreeSet<>();
43     private static final Set<String> BINDABLE_PROPERTIES = new LinkedHashSet<>(
44         Arrays.asList("propertyChangeListeners""vetoableChangeListeners"));
45 
46     public DefaultGriffonModelClass(@Nonnull GriffonApplication application, @Nonnull Class<?> clazz) {
47         super(application, clazz, TYPE, TRAILING);
48     }
49 
50     public void resetCaches() {
51         super.resetCaches();
52         propertiesCache.clear();
53     }
54 
55     @Nonnull
56     public String[] getPropertyNames() {
57         if (propertiesCache.isEmpty()) {
58             for (String propertyName : getPropertiesWithFields()) {
59                 if (!propertiesCache.contains(propertyName&&
60                     !isEventHandler(propertyName&&
61                     !STANDARD_PROPERTIES.contains(propertyName&&
62                     !BINDABLE_PROPERTIES.contains(propertyName)) {
63                     propertiesCache.add(propertyName);
64                 }
65             }
66         }
67 
68         return propertiesCache.toArray(new String[propertiesCache.size()]);
69     }
70 
71     public void setModelPropertyValue(@Nonnull GriffonModel model, @Nonnull String propertyName, @Nullable Object value) {
72         requireNonNull(model, ERROR_MODEL_NULL);
73         requireNonBlank(propertyName, ERROR_PROPERTY_NAME_BLANK);
74         GriffonClassUtils.setPropertyValue(model, propertyName, value);
75     }
76 
77     @Nullable
78     public Object getModelPropertyValue(@Nonnull GriffonModel model, @Nonnull String propertyName) {
79         requireNonNull(model, ERROR_MODEL_NULL);
80         requireNonBlank(propertyName, ERROR_PROPERTY_NAME_BLANK);
81         return GriffonClassUtils.getPropertyValue(model, propertyName);
82     }
83 }