BrightSide Workbench Full Report + Source Code
AccountFormat.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.financials.model;
19 
24 public class AccountFormat {
25 
26  public static final int ACCOUNT_LENGTH = 10;
27 
28  public static String simplify(String a) {
29  if(!isPartialAccount(a)) return a;
30  StringBuilder text = new StringBuilder(a);
31  int i = 4;
32  while(i < text.length() && text.charAt(i) == '0')
33  i++;
34  i--;
35  while(i > -1 && i < text.length() && text.charAt(i) == '0')
36  text.deleteCharAt(i--);
37  if(i < text.length())
38  text.insert(i + 1, '.');
39  return text.toString();
40  }
41 
42  public static String expand(String a) {
43  if((a == null) || (a.length() == 0))
44  return a;
45  StringBuilder text = new StringBuilder(a.trim());
46  int i;
47  if((i = text.lastIndexOf(".")) > -1) {
48  text.deleteCharAt(i);
49  while(text.length() < ACCOUNT_LENGTH)
50  text.insert(i, '0');
51  }
52  while(text.length() < ACCOUNT_LENGTH)
53  text.append('0');
54  while(text.length() > ACCOUNT_LENGTH) {
55  i = text.lastIndexOf("0");
56  if(i > -1)
57  text.deleteCharAt(i);
58  else
59  break;
60  }
61  return text.toString();
62  }
63 
64  public static boolean isAccount(String a) {
65  if(a == null) return false;
66  a = expand(a);
67  if(a.length() != ACCOUNT_LENGTH) return false;
68  for(int i = 0; i < ACCOUNT_LENGTH; i++)
69  if((a.charAt(i) < '0') || (a.charAt(i) > '9'))
70  return false;
71  return true;
72  }
73 
74  public static boolean isPartialAccount(String a) {
75  if(a == null) return false;
76  if(a.length() >= 10) return false;
77  for(int i = 0; i < a.length(); i++)
78  if(((a.charAt(i) < '0') || (a.charAt(i) > '9')) && !(a.charAt(i) == '.'))
79  return false;
80  return true;
81  }
82 
83  public static String trim(String a) {
84  StringBuilder text = new StringBuilder(a);
85  while((text.length() > 0) && (text.charAt(text.length() - 1) == '0'))
86  text.deleteCharAt(text.length() - 1);
87  return text.toString();
88  }
89 
90  private AccountFormat() {
91  }
92 
93 }
static boolean isPartialAccount(String a)