001/* 002 * Copyright 2018 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.runtime.model; 017 018import java.util.HashMap; 019import java.util.Map; 020 021import org.ametys.runtime.model.ModelItem; 022 023/** 024 * Pair of a value and its definition 025 * @param <T> Type of the root object 026 */ 027public class DefinitionAndValue<T> 028{ 029 private T _root; 030 private ModelItem _definition; 031 private Object _value; 032 private DefinitionAndValue<T> _parent; 033 034 /** 035 * Creates a definition and value pair 036 * @param root the root object of this definition and value pair 037 * @param definition the definition 038 * @param value the value 039 */ 040 public DefinitionAndValue(T root, ModelItem definition, Object value) 041 { 042 this(root, definition, value, null); 043 } 044 045 /** 046 * Creates a definition and value pair 047 * @param root the root object of this definition and value pair 048 * @param definition the definition 049 * @param value the value 050 * @param parent the parent of the definition and value pair 051 */ 052 public DefinitionAndValue(T root, ModelItem definition, Object value, DefinitionAndValue<T> parent) 053 { 054 this._root = root; 055 this._definition = definition; 056 this._value = value; 057 this._parent = parent; 058 } 059 060 /** 061 * Retrieves the root object of this definition and value pair 062 * @return the root 063 */ 064 public T getRoot() 065 { 066 return _root; 067 } 068 069 /** 070 * Retrieves the definition 071 * @return the definition 072 */ 073 public ModelItem getDefinition() 074 { 075 return _definition; 076 } 077 078 /** 079 * Retrieves the value 080 * @return the value 081 */ 082 public Object getValue() 083 { 084 return _value; 085 } 086 087 /** 088 * Retrieves the parent of the definition and value pair 089 * @return the parent 090 */ 091 public DefinitionAndValue<T> getParent() 092 { 093 return _parent; 094 } 095 096 /** 097 * extract a map of values from a map of DefinitionAndValue 098 * @param definitionAndValues a map containing definition and values 099 * @return a map containing only values 100 */ 101 public static Map<String, Object> extractValues(Map<String, DefinitionAndValue> definitionAndValues) 102 { 103 Map<String, Object> values = new HashMap<>(); 104 for (Map.Entry<String, DefinitionAndValue> entry : definitionAndValues.entrySet()) 105 { 106 Object value = entry.getValue().getValue(); 107 values.put(entry.getKey(), value); 108 } 109 return values; 110 } 111}