View Javadoc
1   /**
2    * Copyright 2010-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.util.spring.format;
17  
18  import java.util.Locale;
19  
20  import org.kuali.common.util.FormatUtils;
21  import org.kuali.common.util.primitives.Numbers;
22  import org.springframework.format.Formatter;
23  
24  public final class BytesFormatter implements Formatter<Number> {
25  
26  	public BytesFormatter() {
27  		this(true);
28  	}
29  
30  	public BytesFormatter(boolean printDecimalDigits) {
31  		this.printDecimalDigits = printDecimalDigits;
32  	}
33  
34  	public boolean isPrintDecimalDigits() {
35  		return printDecimalDigits;
36  	}
37  
38  	private final boolean printDecimalDigits;
39  
40  	@Override
41  	public Number parse(String size, Locale locale) {
42  		return Numbers.narrow(FormatUtils.getBytes(size));
43  	}
44  
45  	@Override
46  	public String print(Number number, Locale locale) {
47  		if (printDecimalDigits) {
48  			return FormatUtils.getSize(number.longValue());
49  		} else {
50  			return FormatUtils.getIntegerSize(number.longValue());
51  		}
52  	}
53  
54  }