Mixed Radix Number : Number « Data Type « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Data Type » NumberScreenshots 
Mixed Radix Number
      
//package aima.core.util.math;

import java.util.List;

/**
 @author Ciaran O'Reilly see:
 *         http://demonstrations.wolfram.com/MixedRadixNumberRepresentations/
 *         for useful example.
 */
public class MixedRadixNumber extends Number {
  //
  private static final long serialVersionUID = 1L;
  //
  private long value = 0L;
  private long maxValue = 0L;
  private int[] radixs = null;
  private int[] currentNumeralValue = null;
  private boolean recalculate = true;

  public MixedRadixNumber(long value, int[] radixs) {
    this.value = value;
    this.radixs = new int[radixs.length];
    System.arraycopy(radixs, 0this.radixs, 0, radixs.length);
    calculateMaxValue();
  }

  public MixedRadixNumber(long value, List<Integer> radixs) {
    this.value = value;
    this.radixs = new int[radixs.size()];
    for (int i = 0; i < radixs.size(); i++) {
      this.radixs[i= radixs.get(i);
    }
    calculateMaxValue();
  }

  public long getMaxAllowedValue() {
    return maxValue;
  }

  public boolean increment() {
    if (value < maxValue) {
      value++;
      recalculate = true;
      return true;
    }

    return false;
  }

  public boolean decrement() {
    if (value > 0) {
      value--;
      recalculate = true;
      return true;
    }
    return false;
  }

  public int getCurrentNumeralValue(int atPosition) {
    if (atPosition >= && atPosition < radixs.length) {
      if (recalculate) {
        long quotient = value;
        for (int i = 0; i < radixs.length; i++) {
          if (!= quotient) {
            currentNumeralValue[i(intquotient % radixs[i];
            quotient = quotient / radixs[i];
          else {
            currentNumeralValue[i0;
          }

        }
        recalculate = false;
      }
      return currentNumeralValue[atPosition];
    }
    throw new IllegalArgumentException(
        "Argument atPosition must be >=0 and < " + radixs.length);
  }

  //
  // START-Number
  @Override
  public int intValue() {
    return (intlongValue();
  }

  @Override
  public long longValue() {
    return value;
  }

  @Override
  public float floatValue() {
    return longValue();
  }

  @Override
  public double doubleValue() {
    return longValue();
  }

  // END-Number
  //

  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < radixs.length; i++) {
      sb.append("[");
      sb.append(this.getCurrentNumeralValue(i));
      sb.append("]");
    }

    return sb.toString();
  }

  //
  // PRIVATE
  //
  private void calculateMaxValue() {
    if (== radixs.length) {
      throw new IllegalArgumentException(
          "At least 1 radix must be defined.");
    }
    for (int i = 0; i < radixs.length; i++) {
      if (radixs[i2) {
        throw new IllegalArgumentException(
            "Invalid radix, must be >= 2");
      }
    }

    // Calcualte the maxValue allowed
    maxValue = radixs[0];
    for (int i = 1; i < radixs.length; i++) {
      maxValue *= radixs[i];
    }
    maxValue -= 1;

    if (value > maxValue) {
      throw new IllegalArgumentException(
          "The value ["
              + value
              "] cannot be represented with the radixs provided, max value is "
              + maxValue);
    }

    currentNumeralValue = new int[radixs.length];
  }
}

   
    
    
    
    
    
  
Related examples in the same category
1.Methods for number conversion and parsing
2.Get Digit Number String
3.Convert to numbers
4.Check Number properties and convert from Number
5.Check if number are in a given range
6.Various number-related routines and classes that are frequently used
7.Turns a string value into a java.lang.Number.
8.Provides IEEE-754r variants of NumberUtils methods.
9.Checks whether the String a valid Java number.
10.Specifies sizes of various types in bytes.
11.Class to represent 16-bit unsigned integers.
12.Implements an integer object wrapper which allows changing the integer value.Implements an integer object wrapper which allows changing the integer value.
13.This class allows a number to be converted to it's unsigned value.
14.Numbers will be equals if enough decimals are equals, without thinking about approximations.
15.Ternary Numnber
16.Implements a long object wrapper which allows changing the long value.
17.Convert the given number into an instance of the given target class.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.