001    /*
002     * Jpkg - Java library and tools for operating system package creation.
003     *
004     * Copyright (c) 2007-2008 Three Rings Design, Inc.
005     * All rights reserved.
006     *
007     * Redistribution and use in source and binary forms, with or without
008     * modification, are permitted provided that the following conditions
009     * are met:
010     * 1. Redistributions of source code must retain the above copyright
011     *    notice, this list of conditions and the following disclaimer.
012     * 2. Redistributions in binary form must reproduce the above copyright
013     *    notice, this list of conditions and the following disclaimer in the
014     *    documentation and/or other materials provided with the distribution.
015     * 3. Neither the name of the copyright owner nor the names of contributors
016     *    may be used to endorse or promote products derived from this software
017     *    without specific prior written permission.
018     *
019     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
020     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
021     * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
022     * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
023     * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
024     * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
025     * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
026     * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
027     * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028     * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
029     * POSSIBILITY OF SUCH DAMAGE.
030     */
031    package com.threerings.jpkg.ant.dpkg.dependencies;
032    
033    import com.threerings.antidote.field.BaseField;
034    import com.threerings.antidote.field.OptionalField;
035    import com.threerings.antidote.property.StringProperty;
036    import com.threerings.jpkg.ant.dpkg.dependencies.conditions.Condition;
037    import com.threerings.jpkg.debian.dependency.AbstractDependency;
038    
039    import static com.threerings.antidote.MutabilityHelper.areMutablesSet;
040    import static com.threerings.antidote.MutabilityHelper.requiresValidation;
041    
042    /**
043     * Base class for all {@link Dependencies} fields.
044     * @see AbstractDependency
045     */
046    public abstract class BaseDependency<T extends AbstractDependency> extends BaseField
047        implements PackageInfoDependency
048    {
049        /**
050         * Ant setter field: package. The name of the package.
051         */
052        public void setPackage (String value)
053        {
054            _packageName.setValue(value);
055        }
056    
057        /**
058         * Ant adder field: condition. All {@link Condition} objects.
059         */
060        public void add (Condition condition)
061        {
062            _condition.setField(condition);
063        }
064    
065        /**
066         * Construct an instance of the {@link AbstractDependency} defined in this field with just
067         * the package name defined.
068         */
069        protected abstract T createDependency (StringProperty packageName);
070    
071        /**
072         * Construct an instance of the {@link AbstractDependency} defined in this field with a
073         * package name and condition defined.
074         */
075        protected abstract T createDependency (StringProperty packageName, Condition condition);
076    
077        /**
078         * Get the concrete {@link AbstractDependency} after it has been created.
079         */
080        protected T getDependency ()
081        {
082            requiresValidation(_abstractDependency);
083            return _abstractDependency;
084        }
085    
086        @Override // from BaseComponent
087        protected void validateField ()
088        {
089            // validate required properties
090            switch (validateProperties(_packageName)) {
091                case SOME_INVALID:
092                case ALL_INVALID:
093                    return;
094    
095                case ALL_VALID:
096                    break;
097            }
098    
099            // determine if we have a Condition set
100            switch (areMutablesSet(_condition)) {
101                case ALL_UNSET:
102                case SOME_UNSET:
103                    // no condition set, construct the AbstractDependency
104                    _abstractDependency = createDependency(_packageName);
105                    return;
106    
107                case ALL_SET:
108                    // condition set, move on to validation
109                    break;
110            }
111    
112            // validate condition field
113            switch (validateChildFields(_condition)) {
114                case SOME_INVALID:
115                case ALL_INVALID:
116                    return;
117    
118                case ALL_VALID:
119                    _abstractDependency = createDependency(_packageName, _condition.getField());
120                    return;
121            }
122        }
123    
124        /** The AbstractDependency object representing the user supplied data. */
125        private T _abstractDependency;
126    
127        /** Ant adder/setter fields. */
128        private final StringProperty _packageName = new StringProperty("package", this);
129        private final OptionalField<Condition> _condition = new OptionalField<Condition>("condition", this);
130    }