Building Simples

Overview

BSimple is the base class for all atomic data types in Niagara. As an atomic data type, BSimples store a simple piece of data which cannot be decomposed. All simples are immutable, that is once an instance is created it may never change its state. Concrete subclasses of BSimples must meet the following requirements:

Example

The following source provides a example:
  
/*
 * Copyright 2000 Tridium, Inc. All Rights Reserved.
 */
package javax.baja.sys;

import java.io.*;

/**
 * The BInteger is the wrapper class for Java primitive
 * int objects.
 */
public final class BInteger
  extends BNumber
{ 

  public static BInteger make(int value)
  {
    if (value == 0) return DEFAULT;
    return new BInteger(value);
  }

  private BInteger(int value)
  {
    this.value = value;
  }

  public int getInt()
  {
    return value;
  }

  public float getFloat()
  {
    return (float)value;
  }
  
  public int hashCode()
  {
    return value;
  }
  
  public boolean equals(Object obj)
  {
    if (obj instanceof BInteger)
      return ((BInteger)obj).value == value;
    return false;
  }
        
  public String toString(Context context)
  {
    return String.valueOf(value);
  }
  
  public void encode(DataOutput out)
    throws IOException
  {
    out.writeInt(value);
  }
  
  public BObject decode(DataInput in)
    throws IOException
  {
    return new BInteger( in.readInt() );
  }

  public String encodeToString()
    throws IOException
  {
    return String.valueOf(value);
  }

  public BObject decodeFromString(String s)
    throws IOException
  {
    try
    {
      return new BInteger( Integer.parseInt(s) );
    }
    catch(Exception e)
    {
      throw new IOException("Invalid integer: " + s);
    }
  }

  public static final BInteger DEFAULT = new BInteger(0);

  public Type getType() { return TYPE; }
  public static final Type TYPE = Sys.loadType(BInteger.class);
  
  private int value;
}