| 1 | package org.jtoolkit.essence.app.pojo; |
| 2 | |
| 3 | import org.jetbrains.annotations.NotNull; |
| 4 | import org.jtoolkit.essence.app.pojo.impl.DataValueClass; |
| 5 | import org.jtoolkit.essence.concurrency.Immutable; |
| 6 | import org.jtoolkit.essence.utils.Named; |
| 7 | |
| 8 | import java.util.Map; |
| 9 | import java.util.NoSuchElementException; |
| 10 | import java.util.Set; |
| 11 | import java.lang.reflect.InvocationTargetException; |
| 12 | |
| 13 | /* |
| 14 | Copyright 2006 Peter Lawrey |
| 15 | |
| 16 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 17 | you may not use this file except in compliance with the License. |
| 18 | You may obtain a copy of the License at |
| 19 | |
| 20 | http://www.apache.org/licenses/LICENSE-2.0 |
| 21 | |
| 22 | Unless required by applicable law or agreed to in writing, software |
| 23 | distributed under the License is distributed on an "AS IS" BASIS, |
| 24 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 25 | See the License for the specific language governing permissions and |
| 26 | limitations under the License. |
| 27 | */ |
| 28 | |
| 29 | /** |
| 30 | * Root object class for immutable data value classes. |
| 31 | * <p/>A data value class is any immutable class which can be represented as a series of key value pairs |
| 32 | * and has a constructor which initialises all field in the order they are defined. A data value object |
| 33 | * does not need to extend this class instead this class defines a number of useful methods. |
| 34 | * <p/>Note: data value class can have public fields but they should be final. |
| 35 | * |
| 36 | * @author Peter Lawrey |
| 37 | */ |
| 38 | @Immutable |
| 39 | public class DataValue<T extends DataValue> implements Named.Source<Object> { |
| 40 | @SuppressWarnings("DollarSignInName") |
| 41 | private transient int $hashCode = 0; |
| 42 | |
| 43 | /** |
| 44 | * Cannot create a plan data value object with no values. |
| 45 | */ |
| 46 | protected DataValue() { |
| 47 | } |
| 48 | |
| 49 | public boolean equals(Object obj) { |
| 50 | return DataValueClass.equals(this, obj); |
| 51 | } |
| 52 | |
| 53 | @SuppressWarnings("NonFinalFieldReferencedInHashCode") |
| 54 | public int hashCode() { |
| 55 | if ($hashCode == 0) $hashCode = DataValueClass.hashCode(this); |
| 56 | return $hashCode; |
| 57 | } |
| 58 | |
| 59 | public String toString() { |
| 60 | return DataValueClass.toString(this); |
| 61 | } |
| 62 | |
| 63 | @NotNull public Map<String, Object> asMap() { |
| 64 | return DataValueClass.asMap(this); |
| 65 | } |
| 66 | |
| 67 | @SuppressWarnings({"UnusedDeclaration"}) |
| 68 | public T setCopy(@NotNull String field, Object value, Object... others) { |
| 69 | //noinspection unchecked |
| 70 | return (T) DataValueClass.setNew(this, field, value, others); |
| 71 | } |
| 72 | |
| 73 | @SuppressWarnings({"UnusedDeclaration"}) |
| 74 | public <T2 extends DataValue> T2 project(@NotNull Class<T2> class2, Object... keyValues) { |
| 75 | return DataValueClass.project(class2, this, keyValues); |
| 76 | } |
| 77 | |
| 78 | @NotNull public Set<String> getNames() { |
| 79 | return DataValueClass.getFieldNames(getClass()); |
| 80 | } |
| 81 | |
| 82 | public Object getValue(@NotNull String name) throws NoSuchElementException { |
| 83 | try { |
| 84 | return DataValueClass.getField(this, name); |
| 85 | } catch (IllegalAccessException e) { |
| 86 | throw new NoSuchElementException(e.toString()); |
| 87 | } catch (InvocationTargetException e) { |
| 88 | throw new NoSuchElementException(e.toString()); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | @NotNull public String getName() { |
| 93 | try { |
| 94 | return (String) getValue("name"); |
| 95 | } catch (NoSuchElementException ignored) { |
| 96 | return toString(); |
| 97 | } |
| 98 | } |
| 99 | } |