001/**
002 * Copyright 2016 Tampere University of Technology, Pori Department
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 service.tut.pori.apilta.sensors.datatypes;
017
018import java.util.Map;
019import java.util.Map.Entry;
020
021import javax.xml.bind.annotation.XmlAccessType;
022import javax.xml.bind.annotation.XmlAccessorType;
023import javax.xml.bind.annotation.XmlElementWrapper;
024import javax.xml.bind.annotation.XmlRootElement;
025
026import org.apache.commons.lang3.StringUtils;
027
028/**
029 * Answers to the question "when this task is active?"
030 *
031 */
032@XmlRootElement(name=Definitions.ELEMENT_CONDITION)
033@XmlAccessorType(XmlAccessType.NONE)
034public class Condition {
035  @XmlElementWrapper(name = Definitions.ELEMENT_TERMS)
036  private Map<String, String> _conditions = null;
037  
038  /**
039   * @return the conditions
040   * @see #setConditions(Map)
041   */
042  public Map<String, String> getConditions() {
043    return _conditions;
044  }
045  
046  /**
047   * @param conditions the conditions to set
048   * @see #getConditions()
049   */
050  public void setConditions(Map<String, String> conditions) {
051    _conditions = conditions;
052  }
053
054  /**
055   * for sub-classing, use the static
056   * 
057   * @return true if valid
058   * @see #isValid(Condition)
059   */
060  protected boolean isValid() {
061    if(_conditions == null || _conditions.isEmpty()){
062      return false;
063    }
064    
065    for(Entry<String, String> entry : _conditions.entrySet()){
066      if(StringUtils.isBlank(entry.getKey()) || StringUtils.isBlank(entry.getValue())){
067        return false;
068      }
069    }
070    
071    return true;
072  }
073
074  /**
075   * 
076   * @param condition
077   * @return false if condition is null or invalid
078   */
079  public static boolean isValid(Condition condition) {
080    if(condition == null){
081      return false;
082    }else{
083      return condition.isValid();
084    }
085  }
086}