001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *      http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.oozie.coord.input.logic;
020
021import java.io.IOException;
022
023import org.apache.commons.lang.StringUtils;
024
025public class CoordInputLogicBuilder {
026
027    StringBuffer bf = new StringBuffer();
028
029    CoordInputLogicEvaluator coordInputlogicEvaluator;
030
031    /** The Dependency builder. */
032    public CoordInputDependencyBuilder dependencyBuilder;
033
034    public CoordInputLogicBuilder(CoordInputLogicEvaluator coordInputlogicEvaluator) {
035        this.coordInputlogicEvaluator = coordInputlogicEvaluator;
036        dependencyBuilder = new CoordInputDependencyBuilder(coordInputlogicEvaluator);
037    }
038
039    /**
040     * Input function of input-logic
041     *
042     * @param inputDataset the dataset
043     * @return the string
044     */
045    public CoordInputLogicEvaluatorResult input(String inputDataset) {
046        return coordInputlogicEvaluator.evalInput(inputDataset, -1, -1);
047
048    }
049
050    /**
051     * Combine function of dataset
052     *
053     * @param combineDatasets the combine
054     * @return the string
055     */
056    public CoordInputLogicEvaluatorResult combine(String... combineDatasets) {
057        return coordInputlogicEvaluator.evalCombineInput(combineDatasets, -1, -1);
058    }
059
060    /**
061     * The Class CoordInputDependencyBuilder.
062     */
063    public static class CoordInputDependencyBuilder {
064
065        CoordInputLogicEvaluator coordInputLogicEvaluator;
066
067        public CoordInputDependencyBuilder(CoordInputLogicEvaluator coordInputLogicEvaluator) {
068            this.coordInputLogicEvaluator = coordInputLogicEvaluator;
069
070        }
071
072        private int minValue = -1;
073        private String wait;
074        private String inputDataset;
075        private String[] combineDatasets;
076
077        /**
078         * Construct min function
079         *
080         * @param minValue the min value
081         * @return the coord input dependency builder
082         */
083        public CoordInputDependencyBuilder min(int minValue) {
084            this.minValue = minValue;
085            return this;
086        }
087
088        /**
089         * Construct  wait function
090         *
091         * @param wait the wait
092         * @return the coord input dependency builder
093         */
094        public CoordInputDependencyBuilder inputWait(String wait) {
095            this.wait = wait;
096            return this;
097        }
098
099        /**
100         * Construct wait function
101         *
102         * @param wait the wait
103         * @return the coord input dependency builder
104         */
105        public CoordInputDependencyBuilder inputWait(int wait) {
106            this.wait = String.valueOf(wait);
107            return this;
108        }
109
110        /**
111         * Construct input function
112         *
113         * @param dataset the input
114         * @return the coord input dependency builder
115         */
116        public CoordInputDependencyBuilder input(String dataset) {
117            this.inputDataset = dataset;
118            return this;
119        }
120
121        /**
122         * Construct complie function
123         *
124         * @param combineDatasets the combine
125         * @return the coord input dependency builder
126         */
127        public CoordInputDependencyBuilder combine(String... combineDatasets) {
128            this.combineDatasets = combineDatasets;
129            return this;
130        }
131
132        /**
133         * Build inputlogic expression
134         *
135         * @return the string
136         * @throws IOException Signals that an I/O exception has occurred.
137         */
138        public CoordInputLogicEvaluatorResult build() throws IOException {
139            if (combineDatasets != null) {
140                return coordInputLogicEvaluator.evalCombineInput(combineDatasets, minValue, getTime(wait));
141            }
142            else {
143                return coordInputLogicEvaluator.evalInput(inputDataset, minValue, getTime(wait));
144            }
145        }
146
147        /**
148         * Gets the time in min.
149         *
150         * @param value the value
151         * @return the time in min
152         * @throws IOException Signals that an I/O exception has occurred.
153         */
154        private int getTime(String value) throws IOException {
155            if (StringUtils.isEmpty(value)) {
156                return -1;
157            }
158            if (StringUtils.isNumeric(value)) {
159                return Integer.parseInt(value);
160            }
161            else {
162                throw new IOException("Unsupported time : " + value);
163            }
164        }
165    }
166
167}