01 /*
02 * Copyright 2008-2017 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 @Override
51 public void resetCaches() {
52 super.resetCaches();
53 propertiesCache.clear();
54 }
55
56 @Nonnull
57 public String[] getPropertyNames() {
58 if (propertiesCache.isEmpty()) {
59 for (String propertyName : getPropertiesWithFields()) {
60 if (!propertiesCache.contains(propertyName) &&
61 !isEventHandler(propertyName) &&
62 !STANDARD_PROPERTIES.contains(propertyName) &&
63 !BINDABLE_PROPERTIES.contains(propertyName)) {
64 propertiesCache.add(propertyName);
65 }
66 }
67 }
68
69 return propertiesCache.toArray(new String[propertiesCache.size()]);
70 }
71
72 public void setModelPropertyValue(@Nonnull GriffonModel model, @Nonnull String propertyName, @Nullable Object value) {
73 requireNonNull(model, ERROR_MODEL_NULL);
74 requireNonBlank(propertyName, ERROR_PROPERTY_NAME_BLANK);
75 GriffonClassUtils.setPropertyValue(model, propertyName, value);
76 }
77
78 @Nullable
79 public Object getModelPropertyValue(@Nonnull GriffonModel model, @Nonnull String propertyName) {
80 requireNonNull(model, ERROR_MODEL_NULL);
81 requireNonBlank(propertyName, ERROR_PROPERTY_NAME_BLANK);
82 return GriffonClassUtils.getPropertyValue(model, propertyName);
83 }
84 }
|