DefaultGriffonModelClass.java
01 /*
02  * SPDX-License-Identifier: Apache-2.0
03  *
04  * Copyright 2008-2017 the original author or authors.
05  *
06  * Licensed under the Apache License, Version 2.0 (the "License");
07  * you may not use this file except in compliance with the License.
08  * You may obtain a copy of the License at
09  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package org.codehaus.griffon.runtime.core.artifact;
19 
20 import griffon.core.GriffonApplication;
21 import griffon.core.artifact.GriffonModel;
22 import griffon.core.artifact.GriffonModelClass;
23 import griffon.util.GriffonClassUtils;
24 
25 import javax.annotation.Nonnull;
26 import javax.annotation.Nullable;
27 import java.util.Arrays;
28 import java.util.LinkedHashSet;
29 import java.util.Set;
30 import java.util.TreeSet;
31 
32 import static griffon.util.GriffonClassUtils.isEventHandler;
33 import static griffon.util.GriffonNameUtils.requireNonBlank;
34 import static java.util.Objects.requireNonNull;
35 
36 /**
37  @author Andres Almiray
38  @since 2.0.0
39  */
40 public class DefaultGriffonModelClass extends DefaultGriffonClass implements GriffonModelClass {
41     private static final String ERROR_MODEL_NULL = "Argument 'model' must not be null";
42     private static final String ERROR_PROPERTY_NAME_BLANK = "Argument 'propertyName' must not be blank";
43 
44     protected final Set<String> propertiesCache = new TreeSet<>();
45     private static final Set<String> BINDABLE_PROPERTIES = new LinkedHashSet<>(
46         Arrays.asList("propertyChangeListeners""vetoableChangeListeners"));
47 
48     public DefaultGriffonModelClass(@Nonnull GriffonApplication application, @Nonnull Class<?> clazz) {
49         super(application, clazz, TYPE, TRAILING);
50     }
51 
52     @Override
53     public void resetCaches() {
54         super.resetCaches();
55         propertiesCache.clear();
56     }
57 
58     @Nonnull
59     public String[] getPropertyNames() {
60         if (propertiesCache.isEmpty()) {
61             for (String propertyName : getPropertiesWithFields()) {
62                 if (!propertiesCache.contains(propertyName&&
63                     !isEventHandler(propertyName&&
64                     !STANDARD_PROPERTIES.contains(propertyName&&
65                     !BINDABLE_PROPERTIES.contains(propertyName)) {
66                     propertiesCache.add(propertyName);
67                 }
68             }
69         }
70 
71         return propertiesCache.toArray(new String[propertiesCache.size()]);
72     }
73 
74     public void setModelPropertyValue(@Nonnull GriffonModel model, @Nonnull String propertyName, @Nullable Object value) {
75         requireNonNull(model, ERROR_MODEL_NULL);
76         requireNonBlank(propertyName, ERROR_PROPERTY_NAME_BLANK);
77         GriffonClassUtils.setPropertyValue(model, propertyName, value);
78     }
79 
80     @Nullable
81     public Object getModelPropertyValue(@Nonnull GriffonModel model, @Nonnull String propertyName) {
82         requireNonNull(model, ERROR_MODEL_NULL);
83         requireNonBlank(propertyName, ERROR_PROPERTY_NAME_BLANK);
84         return GriffonClassUtils.getPropertyValue(model, propertyName);
85     }
86 }