001    /*
002     * The MIT License
003     * Copyright (c) 2012 Microsoft Corporation
004     *
005     * Permission is hereby granted, free of charge, to any person obtaining a copy
006     * of this software and associated documentation files (the "Software"), to deal
007     * in the Software without restriction, including without limitation the rights
008     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
009     * copies of the Software, and to permit persons to whom the Software is
010     * furnished to do so, subject to the following conditions:
011     *
012     * The above copyright notice and this permission notice shall be included in
013     * all copies or substantial portions of the Software.
014     *
015     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
016     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
017     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
018     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
019     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
020     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
021     * THE SOFTWARE.
022     */
023    
024    package microsoft.exchange.webservices.data.property.complex;
025    
026    import microsoft.exchange.webservices.data.core.EwsServiceXmlReader;
027    import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
028    import microsoft.exchange.webservices.data.core.XmlElementNames;
029    import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
030    import microsoft.exchange.webservices.data.core.exception.service.local.ServiceValidationException;
031    import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException;
032    
033    import javax.xml.stream.XMLStreamException;
034    
035    /**
036     * Represents the minimum and maximum size of a message.
037     */
038    public final class RulePredicateSizeRange extends ComplexProperty {
039      /**
040       * Minimum Size.
041       */
042      private Integer minimumSize;
043    
044      /**
045       * Mamixmum Size.
046       */
047      private Integer maximumSize;
048    
049      /**
050       * Initializes a new instance of the RulePredicateSizeRange class.
051       */
052      protected RulePredicateSizeRange() {
053        super();
054      }
055    
056      /**
057       * Gets or sets the minimum size, in kilobytes.
058       * If MinimumSize is set to null, no minimum size applies.
059       */
060      public Integer getMinimumSize() {
061    
062        return this.minimumSize;
063      }
064    
065      public void setMinimumSize(Integer value) {
066        if (this.canSetFieldValue(this.minimumSize, value)) {
067          this.minimumSize = value;
068          this.changed();
069        }
070      }
071    
072      /**
073       * Gets or sets the maximum size, in kilobytes.
074       * If MaximumSize is set to null, no maximum size applies.
075       */
076      public Integer getMaximumSize() {
077        return this.maximumSize;
078      }
079    
080      public void setMaximumSize(Integer value) {
081        if (this.canSetFieldValue(this.maximumSize, value)) {
082          this.maximumSize = value;
083          this.changed();
084        }
085    
086      }
087    
088    
089      /**
090       * Tries to read element from XML.
091       *
092       * @param reader The reader.
093       * @return True if element was read.
094       */
095      @Override
096      public boolean tryReadElementFromXml(EwsServiceXmlReader reader)
097          throws Exception {
098    
099        if (reader.getLocalName().equalsIgnoreCase(XmlElementNames.MinimumSize)) {
100          this.minimumSize = reader.readElementValue(Integer.class);
101          return true;
102        } else if (reader.getLocalName().equalsIgnoreCase(XmlElementNames.MaximumSize)) {
103          this.maximumSize = reader.readElementValue(Integer.class);
104          return true;
105        } else {
106          return false;
107        }
108    
109      }
110    
111      /**
112       * Writes elements to XML.
113       *
114       * @param writer the writer
115       * @throws XMLStreamException the XML stream exception
116       */
117      @Override
118      public void writeElementsToXml(EwsServiceXmlWriter writer)
119          throws ServiceXmlSerializationException, XMLStreamException {
120        if (this.getMinimumSize() != null) {
121          writer.writeElementValue(XmlNamespace.Types,
122              XmlElementNames.MinimumSize, this.getMinimumSize());
123        }
124        if (this.getMaximumSize() != null) {
125          writer.writeElementValue(XmlNamespace.Types,
126              XmlElementNames.MaximumSize, this.getMaximumSize());
127        }
128      }
129    
130      /**
131       * Validates this instance.
132       */
133      @Override
134      protected void internalValidate()
135          throws ServiceValidationException, Exception {
136        super.internalValidate();
137        if (this.minimumSize != null &&
138            this.maximumSize != null &&
139            this.minimumSize > this.maximumSize) {
140          throw new ServiceValidationException(
141              "MinimumSize cannot be larger than MaximumSize.");
142        }
143      }
144    }
145    
146    
147