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;
17  
18  import org.codehaus.plexus.util.StringUtils;
19  import org.junit.Test;
20  import org.slf4j.Logger;
21  import org.slf4j.LoggerFactory;
22  
23  public class FormatUtils2Test {
24  
25  	private static final Logger logger = LoggerFactory.getLogger(FormatUtils2Test.class);
26  
27  	@Test
28  	public void test1() {
29  		try {
30  			long number = 2;
31  			for (int i = 0; i < 55; i++) {
32  				long pow = (long) Math.pow(number, i);
33  				String time = FormatUtils.getTime(pow);
34  				String size = FormatUtils.getSize(pow);
35  				System.out.print("pow=" + lpad(FormatUtils.getCount(pow), 23) + "   time=" + lpad(time, 10) + "    size=" + lpad(size, 8));
36  				long millis = FormatUtils.getMillis(time);
37  				long bytes = FormatUtils.getBytes(size);
38  				System.out.println("    time=" + lpad(FormatUtils.getCount(millis), 23) + "    size=" + lpad(FormatUtils.getCount(bytes), 23));
39  			}
40  		} catch (Exception e) {
41  			e.printStackTrace();
42  		}
43  	}
44  
45  	@Test
46  	public void test() {
47  		show("1");
48  		show("1b");
49  		show("1k");
50  		show("1m");
51  		show("1g");
52  		show("1t");
53  		show("1p");
54  		show("1e");
55  		System.out.println();
56  		show("1.378g");
57  		show("1.921t");
58  	}
59  
60  	protected void show(String size) {
61  		long bytes = FormatUtils.getBytes(size);
62  		String original = lpad(size, 6);
63  		String formatted = lpad(FormatUtils.getSize(bytes), 7);
64  		String raw = lpad(FormatUtils.getCount(bytes), 25);
65  		Object[] args = { original, formatted, raw };
66  		logger.info("{} - {} - {}", args);
67  	}
68  
69  	protected String lpad(String s, int size) {
70  		return StringUtils.leftPad(s, size, " ");
71  	}
72  
73  	protected String lpad(long number, int size) {
74  		return lpad(number + "", size);
75  	}
76  }