001/**
002 * Copyright 2014 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.facebookjazz;
017
018import java.util.HashSet;
019import java.util.Set;
020
021import javax.xml.bind.annotation.XmlAccessType;
022import javax.xml.bind.annotation.XmlAccessorType;
023import javax.xml.bind.annotation.XmlElement;
024import javax.xml.bind.annotation.XmlRootElement;
025
026import core.tut.pori.http.ResponseData;
027import service.tut.pori.facebookjazz.WeightModifier.WeightModifierType;
028
029/**
030 * The list of weight modifiers. This class can be directly used in a Response.
031 * 
032 * <h3>XML Example</h3>
033 * 
034 * {@doc.restlet service="[service.tut.pori.facebookjazz.reference.Definitions#SERVICE_FBJ_REFERENCE_EXAMPLE]" method="[service.tut.pori.facebookjazz.Definitions#ELEMENT_WEIGHT_MODIFIER_LIST]" type="GET" query="" body_uri=""}
035 * 
036 * @see service.tut.pori.facebookjazz.WeightModifier
037 */
038@XmlRootElement(name=Definitions.ELEMENT_WEIGHT_MODIFIER_LIST)
039@XmlAccessorType(XmlAccessType.NONE)
040public class WeightModifierList extends ResponseData {
041  @XmlElement(name=Definitions.ELEMENT_WEIGHT_MODIFIER)
042  private Set<WeightModifier> _modifiers = null;
043
044  /**
045   * for sub-classing, use the static
046   * 
047   * @return true if the list is valid
048   * @see #isValid(WeightModifierList)
049   */
050  protected boolean isValid(){
051    if(isEmpty()){
052      return false;
053    }
054    for(WeightModifier wm : _modifiers){
055      if(!WeightModifier.isValid(wm)){
056        return false;
057      }
058    }
059    return true;
060  }
061  
062  /**
063   * for sub-classing, use the static
064   * 
065   * @return true if the list is empty
066   * @see #isEmpty(WeightModifierList)
067   */
068  protected boolean isEmpty(){
069    return (_modifiers == null ? true : _modifiers.isEmpty());
070  }
071  
072  /**
073   * 
074   * @param list
075   * @return false if list is null, empty or contains invalid modifiers
076   */
077  public static boolean isValid(WeightModifierList list){
078    return (list == null ? false : list.isValid());
079  }
080  
081  /**
082   * 
083   * @param type
084   * @return the modifier value for the given type or null if not found
085   * @see #setWeightModifier(WeightModifier)
086   */
087  public Integer getModifier(WeightModifierType type){
088    if(type == null || isEmpty()){
089      return null;
090    }
091    for(WeightModifier wm : _modifiers){
092      if(type.equals(wm.getType())){
093        return wm.getValue();
094      }
095    }
096    return null;
097  }
098  
099  /**
100   * 
101   * @param list
102   * @return true if list is null or empty
103   */
104  public static boolean isEmpty(WeightModifierList list){
105    return (list == null ? true : list.isEmpty());
106  }
107
108  /**
109   * 
110   * @param weightModifier
111   * @see #getModifier(service.tut.pori.facebookjazz.WeightModifier.WeightModifierType)
112   */
113  public void setWeightModifier(WeightModifier weightModifier) {
114    if(weightModifier == null){
115      return;
116    }
117    if(_modifiers == null){
118      _modifiers = new HashSet<>();
119    }
120    _modifiers.add(weightModifier);
121  }
122
123  /**
124   * 
125   * @return the modifiers
126   * @see #setWeightModifier(WeightModifier)
127   */
128  public Set<WeightModifier> getModifiers() {
129    return _modifiers;
130  }
131}