1 /****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19
20 package org.apache.jsieve;
21
22 import org.apache.jsieve.parser.generated.Token;
23
24 /**
25 * <p>
26 * A parsed representation of the RFC3028 BNF...
27 * </p>
28 *
29 * <code>1*DIGIT [QUANTIFIER]</code>
30 *
31 * <p>
32 * Note that the stored value is the absolute value after applying the
33 * quantifier.
34 * </p>
35 *
36 */
37 public class NumberArgument implements Argument {
38
39 /**
40 * The absolute value of the number after applying the quentifier.
41 */
42 private Integer fieldValue;
43
44 /**
45 * Constructor for NumberArgument.
46 */
47 private NumberArgument() {
48 super();
49 }
50
51 /**
52 * Constructor for NumberArgument.
53 *
54 * @param token
55 */
56 public NumberArgument(Token token) {
57 this();
58 setValue(token);
59 }
60
61 /**
62 * Sets the value of the reciver to an Integer.
63 *
64 * @param number
65 * The value to set
66 */
67 protected void setValue(Integer number) {
68 fieldValue = number;
69 }
70
71 /**
72 * @see org.apache.jsieve.Argument#getValue()
73 */
74 public Object getValue() {
75 return fieldValue;
76 }
77
78 /**
79 * Method getInteger answers the value of the receiver as an Integer.
80 *
81 * @return Integer
82 */
83 public Integer getInteger() {
84 return fieldValue;
85 }
86
87 /**
88 * Sets the value of the receiver from a Token.
89 *
90 * @param aToken
91 * The Token from which to extract the value to set
92 */
93 protected void setValue(Token aToken) {
94 int endIndex = aToken.image.length();
95 int magnitude = 1;
96 if (aToken.image.endsWith("K")) {
97 magnitude = 1024;
98 endIndex--;
99 } else if (aToken.image.endsWith("M")) {
100 magnitude = 1048576;
101 endIndex--;
102 } else if (aToken.image.endsWith("G")) {
103 magnitude = 1073741824;
104 endIndex--;
105 }
106
107 setValue(new Integer(Integer.parseInt(aToken.image.substring(0,
108 endIndex))
109 * magnitude));
110 }
111
112 /**
113 * @see java.lang.Object#toString()
114 */
115 public String toString() {
116 return (getValue() == null) ? "null" : getValue().toString();
117 }
118
119 }