Files
Brainstorm/Foundation/src/com/foundation/view/FormatNumber.java

160 lines
3.7 KiB
Java
Raw Normal View History

2014-05-30 10:31:51 -07:00
package com.foundation.view;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.text.DecimalFormat;
import java.text.Format;
import java.text.NumberFormat;
import com.common.comparison.Comparator;
/**
* Copyright Declarative Engineering LLC 2009<p>
* Encapsulates the information necessary to setup a number formatter.
*/
public class FormatNumber extends AbstractFormat {
public static final int TYPE_NUMBER = 0;
public static final int TYPE_INTEGER = 1;
public static final int TYPE_PERCENT = 2;
public static final int TYPE_CURRENCY = 3;
private String pattern = null;
private int type = 0;
/**
* FormatNumber constructor.
*/
public FormatNumber() {
}//FormatNumber()//
/**
* FormatNumber constructor.
* @param pattern The pattern to use for formatting the number.
*/
public FormatNumber(String pattern) {
if(pattern == null || pattern.length() == 0) {
throw new IllegalArgumentException("Bad number pattern.");
}//if//
this.pattern = pattern;
}//FormatNumber()//
/**
* FormatNumber constructor.
* @param type The pre-defined number pattern type.
*/
public FormatNumber(int type) {
if(type < 0 || type > TYPE_CURRENCY) {
throw new IllegalArgumentException("Bad number type.");
}//if//
this.type = type;
}//FormatNumber()//
/* (non-Javadoc)
* @see com.foundation.view.IJefFormat#createFormat()
*/
public Format createFormat() {
Format result;
if(pattern != null) {
result = new DecimalFormat(pattern);
}//if//
else {
switch(type) {
case TYPE_NUMBER: {
result = NumberFormat.getNumberInstance();
break;
}//case//
case TYPE_INTEGER: {
result = NumberFormat.getIntegerInstance();
break;
}//case//
case TYPE_CURRENCY: {
result = NumberFormat.getCurrencyInstance();
break;
}//case//
case TYPE_PERCENT: {
result = NumberFormat.getPercentInstance();
break;
}//case//
default: {
result = NumberFormat.getInstance();
break;
}//default//
}//switch//
}//else//
return result;
}//createFormat()//
/* (non-Javadoc)
* @see java.io.Externalizable#readExternal(java.io.ObjectInput)
*/
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
pattern = in.readUTF();
type = in.readInt();
}//readExternal()//
/* (non-Javadoc)
* @see java.io.Externalizable#writeExternal(java.io.ObjectOutput)
*/
public void writeExternal(ObjectOutput out) throws IOException {
out.writeUTF(pattern);
out.writeInt(type);
}//writeExternal()//
/* (non-Javadoc)
* @see java.lang.Object#clone()
*/
public Object clone() throws CloneNotSupportedException {
FormatNumber result = new FormatNumber();
result.pattern = pattern;
result.type = type;
return result;
}//clone()//
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object object) {
return (object instanceof FormatNumber) && (Comparator.equals(pattern, ((FormatNumber) object).pattern)) && (type == ((FormatNumber) object).type);
}//equals()//
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
public int hashCode() {
return pattern == null ? type : pattern.hashCode();
}//hashCode()//
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString() {
String result = "number ";
if(pattern != null) {
result += pattern;
}//if//
else {
switch(type) {
case TYPE_NUMBER: {
result += "number";
break;
}//case//
case TYPE_INTEGER: {
result += "integer";
break;
}//case//
case TYPE_CURRENCY: {
result += "currency";
break;
}//case//
case TYPE_PERCENT: {
result += "percent";
break;
}//case//
default: {
result += "number";
break;
}//default//
}//switch//
}//else//
return result;
}//toString()//
}//FormatNumber//