001/*
002 *  Copyright 2020 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.Optional;
019
020import org.apache.commons.lang3.StringUtils;
021
022/**
023 * Object that gives some context for definitions manipulation
024 */
025public final class DefinitionContext
026{
027    private Optional<Object> _object = Optional.empty();
028    private boolean _isEdition;
029    private Optional<String> _itemTagName = Optional.empty();
030
031    private DefinitionContext()
032    {
033        // Empty private constructor
034    }
035    
036    /**
037     * Creates a new instance of a {@link DefinitionContext}
038     * @return the created instance
039     */
040    public static DefinitionContext newInstance()
041    {
042        return new DefinitionContext();
043    }
044    
045    /**
046     * Retrieves the context object for the definition
047     * @return the context object
048     */
049    public Optional<Object> getObject()
050    {
051        return _object;
052    }
053    
054    /**
055     * Set the context object
056     * @param object the context object to set
057     * @return the current {@link DefinitionContext}
058     */
059    public DefinitionContext withObject(Object object)
060    {
061        _object = Optional.ofNullable(object);
062        return this;
063    }
064    
065    /**
066     * Checks if the context is in edition
067     * @return <code>true</code> if the context is in edition, <code>false</code> otherwise
068     */
069    public boolean isEdition()
070    {
071        return _isEdition;
072    }
073    
074    /**
075     * Set to <code>true</code> if the context is in edition (default to <code>false</code>) 
076     * @param isEdition <code>true</code> if the context is in edition, <code>false</code> otherwise
077     * @return the current {@link DefinitionContext}
078     */
079    public DefinitionContext withEdition(boolean isEdition)
080    {
081        _isEdition = isEdition;
082        return this;
083    }
084    
085    /**
086     * Retrieves the tag name to use while generating SAX events
087     * @return the tag name to use while generating SAX events
088     */
089    public Optional<String> getItemTagName()
090    {
091        return _itemTagName;
092    }
093    
094    /**
095     * Set the tag name to use while generating SAX events
096     * @param itemTagName the tag name to set
097     * @return the current {@link DefinitionContext}
098     */
099    public DefinitionContext withItemTagName(String itemTagName)
100    {
101        _itemTagName = Optional.ofNullable(itemTagName)
102                               .filter(StringUtils::isNotEmpty);
103        return this;
104    }
105}