001    /*
002     * Copyright (c) 1998-2014 ChemAxon Ltd. All Rights Reserved.
003     *
004     * This software is the confidential and proprietary information of
005     * ChemAxon. You shall not disclose such Confidential Information
006     * and shall use it only in accordance with the terms of the agreements
007     * you entered into with ChemAxon.
008     *
009     */
010    
011    package com.chemaxon.apidiscovery;
012    
013    import com.chemaxon.apidiscovery.annotations.Description;
014    import com.chemaxon.apidiscovery.interfaces.ParameterBuilder;
015    import com.chemaxon.common.annotations.PublicAPI;
016    import com.google.common.annotations.Beta;
017    import com.google.common.base.Optional;
018    import org.apache.commons.logging.Log;
019    import org.apache.commons.logging.LogFactory;
020    
021    /**
022     * Wrapper for Instances.
023     *
024     * <p>Note that usually instances actual are immutable (because of being parameter objects or enums). Some of
025     * them have associated builder classes.</p>
026     *
027     * <p>Please note that this class is marked with @Beta annotation, so it can be subject of incompatible changes or
028     * removal in later releases.</p>
029     *
030     * @param <T>   Type of wrapped instance
031     *
032     * @author Gabor Imre
033     */
034    @Beta
035    @PublicAPI
036    public class InstanceWrapper<T> {
037    
038        /**
039         * Logger to be used
040         */
041        private final Log log = LogFactory.getLog(InstanceWrapper.class);
042    
043        /**
044         * Wrapped instance
045         */
046        private final T t;
047    
048        /**
049         * Description of the wrapped instance
050         */
051        private final Description desc;
052    
053        /**
054         * Associated parameter builder if exists
055         */
056        private final Optional<ParameterBuilder<T>> builder;
057    
058        /**
059         * Create a wrapper for an instance
060         *
061         * @param t     The wrapped instance
062         */
063        InstanceWrapper(T t) {
064            if (this.log.isDebugEnabled()) {
065                this.log.debug("Wrap instance of " + t.getClass().toString() + ": " + t.toString());
066            }
067            this.t = t;
068            this.desc = Discovery.getDescriptionAnnotation(t);
069            this.builder = Discovery.initializeBuilderIfExists(t);
070        }
071    
072        /**
073         * Get the actual wrapped instance.
074         *
075         * <p>Please note that instance might be immutable, in this case associated builder should be used.</p>
076         *
077         * @return  Wrapped instance
078         */
079        public T get() {
080            return this.t;
081        }
082    
083        /**
084         * Get an associated builder for the given instance if exists.
085         *
086         * @return associated builder
087         */
088        public Optional<ParameterBuilder<T>> getBuilder() {
089            return this.builder;
090        }
091    
092        /**
093         * Represented instance short name - from its Description annotation.
094         *
095         * @return  Short name
096         */
097        public String getShortName() {
098            return this.desc.shortName();
099        }
100    
101        /**
102         * Represented instance name - from its Description annotation.
103         *
104         * @return  Name
105         */
106        public String getName() {
107            return this.desc.name();
108        }
109    
110        /**
111         * Represented instance description - from its description annotation.
112         *
113         * @return Description of the represented instance
114         */
115        public String getDescription() {
116            return this.desc.description();
117        }
118    
119        /**
120         * Represented instances description annotation.
121         *
122         * @return Represented instances description annotation
123         */
124        Description getDescriptionAnnotation() {
125            return this.desc;
126        }
127    }