Skip to content

Commit

Permalink
Partial #33 and partial #11
Browse files Browse the repository at this point in the history
  • Loading branch information
Alok Shukla committed Nov 23, 2013
1 parent 12fd6ab commit 4812db2
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ public interface BigNumberFactory {
public BigNumber getBigNumber();

/**
* It returns the BigNumber which was put on-hold respective to the provided key<br/>
* In case the key is wrong and/or no BigNumber exists corresponding to it a new BigNumber would be returned.<br/>
* Make sure that you do handle it as well.
* @author Nishi Inc.
* @since v1.1.0
* @param key
* @param key Integer to get corresponding BigNumber
* @return A BigNumber object which is on-hold corresponding to the given key
*/
public BigNumber getBigNumber(Integer key);
Expand All @@ -41,5 +44,17 @@ public interface BigNumberFactory {
* @param bignums BigNumbers to return back to pool
*/
public void destroy(BigNumber... bignums);

/**
* Puts given BigNumber on-hold.<br/>
* If the given number was not initially allocated, it's added to pool and put at hold.<br/>
* DO NOT FORGET TO DESTROY NUMBERS ON-HOLD.
* @author Nishi Inc.
* @since v1.1.0
* @param bignum Put this bigNumber on-hold
* @return A 'key' by which this particular number can be grabbed from the pool<br>
* Just call getBigNumber(key)
*/
public Integer hold(BigNumber bignum);

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public final class BigNumberUtils implements StaticMethods {
// TODO Keep two interfaces StaticMethods and NonStaticMethods
// TODO #33 : BigNumberUtil will implement StaticMethods while BigNumberParent will implement NonStaticMethods which extends Serializable and Comparable<BigNumber>
// TODO Make necessary changes in Tests
// TODO Set default mathContext in BigNUmber constructor

// Singleton
private BigNumberUtils(){}
Expand Down
27 changes: 9 additions & 18 deletions src/main/java/org/bigNumber/models/BigNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,13 @@
*/
public class BigNumber extends BigNumberParent {

private static final Integer DEFAULT_ROUND_OFF_DIGITS = 2;

private static final String FROM = "From BigNumber.";
private static final String APPEND = "append";
private static final String INSERT = "insert";
private static final String MODIFY = "modify";
private static final String SET_VALUE = "setValue";
private static final String PUT_AT_FIRST = "putAtFirst";

private List<Character> value;
private BigInteger bigInteger;
private boolean isNegative = false;
private boolean isFractional = false;
private boolean isZero = false;
private Integer locationOfDecimal;

private static final String FROM = "From BigNumber.";
private static final String APPEND = "append";
private static final String INSERT = "insert";
private static final String MODIFY = "modify";
private static final String SET_VALUE = "setValue";
private static final String PUT_AT_FIRST = "putAtFirst";
private static final Integer DEFAULT_ROUND_OFF_DIGITS = 2;

//____________________________________________________________________________________
//********************************* CONSTRUCTORS *************************************
Expand All @@ -60,8 +51,8 @@ public BigNumber(){
}

/**
*
* @param number
* Create a copy from the given BigNumber
* @param number A BigNumber
* @author Nishi Inc.
* @since v0.1.0
*/
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/org/bigNumber/models/BigNumberMathContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,21 @@
public class BigNumberMathContext {

private static final int DEFAULT_NUMBER_OF_DIGITS_AFTER_DECIMAL = 6;
private static final RoundingMode DEFUALT_ROUNDING_MODE = RoundingMode.HALF_EVEN;

private RoundingMode roundingMode;
private Integer numberOfDigitsAfterDecimal;
private int precision;

/**
* Defualt constructor
*/
public BigNumberMathContext() {
this.roundingMode = BigNumberMathContext.DEFUALT_ROUNDING_MODE;
this.precision = MathContext.UNLIMITED.getPrecision();
this.setNumberOfDigitsAfterDecimal(BigNumberMathContext.DEFAULT_NUMBER_OF_DIGITS_AFTER_DECIMAL);
}

/**
*
* @param roundingMode To set roundingMode
Expand All @@ -29,6 +39,16 @@ public BigNumberMathContext(RoundingMode roundingMode) {
this.setNumberOfDigitsAfterDecimal(BigNumberMathContext.DEFAULT_NUMBER_OF_DIGITS_AFTER_DECIMAL);
}

/**
*
* @param mathContext mathContext object to instantiate BigNumberMathContext object
*/
public BigNumberMathContext(MathContext mathContext) {
this.roundingMode = mathContext.getRoundingMode();
this.precision = mathContext.getPrecision();
this.setNumberOfDigitsAfterDecimal(BigNumberMathContext.DEFAULT_NUMBER_OF_DIGITS_AFTER_DECIMAL);
}

public Integer getNumberOfDigitsAfterDecimal() {
return numberOfDigitsAfterDecimal;
}
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/org/bigNumber/models/BigNumberParent.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.util.List;

Expand All @@ -30,8 +31,14 @@ public abstract class BigNumberParent implements NonStaticMethods {

protected static final long serialVersionUID = 1L;

protected BigDecimal bigDecimal;
protected BigNumberMathContext defaultBigNumberMathContext;
protected boolean isNegative = false;
protected boolean isFractional = false;
protected boolean isZero = false;
protected Integer locationOfDecimal;
protected BigInteger bigInteger;
protected BigDecimal bigDecimal;
protected List<Character> value;
protected BigNumberMathContext defaultBigNumberMathContext;

protected BigDecimal getBigDecimal() {
if(bigDecimal == null) {
Expand All @@ -53,6 +60,9 @@ protected void setBigDecimal(BigDecimal bigDecimal) {
this.syncFromDecimal();
}

/**
* Sets all the fields of BigNumber as per the value of BigDecimal field
*/
protected abstract void syncFromDecimal();

}
24 changes: 9 additions & 15 deletions src/main/java/org/bigNumber/models/BigNumberPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class BigNumberPool implements BigNumberFactory {
private TreeMap<Integer, BigNumber> hold;

/**
* Constructs a pool with GlobalConstants.DEFAULT_CAPACITY and GlobalConstants.DEFAULT_LOAD_FACOTR<br/>
* Constructs a pool with DEFAULT_CAPACITY and DEFAULT_LOAD_FACOTR<br/>
* However, this pool can intelligently modify its <b>capacity</b> and <b>loadFactor</b> in runtime
* @author Nishi Inc.
* @since v1.0.0
Expand Down Expand Up @@ -110,7 +110,7 @@ public void destroy(BigNumber... bignums) {
* Destroys BigNumber in pool
* @author Nishi Inc.
* @since v1.0.0
* @param bignum
* @param bignum BigNumber to get rid of
*/
private void destroy(BigNumber bignum) {
boolean removedFromAllotted = this.getAllotted().remove(bignum);
Expand All @@ -129,16 +129,7 @@ private void destroy(BigNumber bignum) {
}
}

/**
* Puts given BigNumber on-hold.<br/>
* If the given number was not initially allocated, it's added to pool and put at hold.<br/>
* DO NOT FORGET TO DESTROY NUMBERS ON-HOLD.
* @author Nishi Inc.
* @since v1.1.0
* @param bignum
* @return A 'key' by which this particular number can be grabbed from the pool<br>
* Just call getBigNumber(key)
*/
@Override
public Integer hold(BigNumber bignum) {
Integer key = nextKey();
this.getAllotted().remove(bignum);
Expand All @@ -147,8 +138,8 @@ public Integer hold(BigNumber bignum) {
}

/**
* Manages BigNumber pool as per defined capacity,
* <br/>loadFactor and currently allotted and free BigNumbers.
* Manages BigNumber pool as per defined capacity,<br/>
* loadFactor and currently allotted and free BigNumbers.
*/
private void managePool() {
this.updateIndex();
Expand Down Expand Up @@ -291,7 +282,10 @@ private TreeMap<Integer, BigNumber> getHold() {
}
return hold;
}


/**
* @return nextKey or <code>++key</code>
*/
private Integer nextKey() {
return ++key;
}
Expand Down

0 comments on commit 4812db2

Please sign in to comment.