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.type;
017
018import org.apache.avalon.framework.configuration.Configuration;
019import org.apache.avalon.framework.configuration.ConfigurationException;
020
021import org.ametys.runtime.model.exception.BadItemTypeException;
022
023/**
024 * Interface for element types
025 * @param <T> Type of the element value
026 */
027public interface ElementType<T> extends ModelItemType
028{
029    /**
030     * Cast an untyped value (string) to an object of the corresponding type
031     * @param value the value to cast
032     * @return An object of the parameterized type representing the given value. Returns null if value cannot be cast
033     * @throws BadItemTypeException if the String value can't be cast to the type
034     */
035    public T castValue(String value) throws BadItemTypeException;
036    
037    /**
038     * Cast a typed value to a String
039     * @param value the value to cast
040     * @return the String representation of the value
041     */
042    public String toString(T value);
043    
044    /**
045     * Convert the value into a JSON object to use client side
046     * @param value the value to convert
047     * @return The value as JSON
048     */
049    public Object valueToJSONForClient(Object value);
050    
051    /**
052     * Convert the given client side JSON object to the types value 
053     * @param json the JSON object to convert
054     * @return the typed value corresponding to the JSON object
055     */
056    public Object fromJSONForClient(Object json);
057    
058    /**
059     * Parses the given configuration to get the typed value
060     * @param configuration the configuration to parse
061     * @return The typed value in the configuration
062     * @throws ConfigurationException if an error occurs while parsing the configuration
063     */
064    public T parseConfiguration(Configuration configuration) throws ConfigurationException;
065    
066    /**
067     * Determines if this type is simple or not.
068     * A simple element type is a type of elements that can be edited in a grid
069     * @return <code>true</code> if the type is simple, <code>false</code> otherwise
070     */
071    public boolean isSimple();
072    
073    /**
074     * Get the class managed by the implementation
075     * @return The class managed (T.class)
076     */
077    public Class getManagedClass();
078
079    /**
080     * Get the class representing an array of managed class by the implementation
081     * @return The class managed (T[].class)
082     */
083    public Class getManagedClassArray();
084    
085    /**
086     * Checks if the value is compatible with the element type 
087     * @param value the value to check
088     * @return <code>true</code> if the value matches the current type, <code>false</code> otherwise
089     */
090    public boolean isCompatible(Object value);
091}