File

packages/java.lang/src/jchar_primitive.ts

Description

The char data type is a single 16-bit Unicode character.
It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).

Note: To retrieve the actual numeric value wrapped in a Jchar you have to use .value syntax.

Constructor

constructor(value: string | Jint)

Methods

Public toString
toString()
Returns: string
Public eq
eq(expr: Jint | Jchar | Jdouble)
Returns: Jboolean
Public ne
ne(expr: Jint | Jchar | Jdouble)
Returns: Jboolean
Public lt
lt(expr: Jint | Jchar | Jdouble)
Returns: Jboolean
Public gt
gt(expr: Jint | Jchar | Jdouble)
Returns: Jboolean
Public le
le(expr: Jint | Jchar | Jdouble)
Returns: Jboolean
Public ge
ge(expr: Jint | Jchar | Jdouble)
Returns: Jboolean
Public plus
plus()
Returns: Jint
Public inc
inc()
Returns: Jchar
Public dec
dec()
Returns: Jchar
Public minus
minus()
Returns: Jint
Public add
add(expr: Jint | Jchar)
Returns: Jint
Public add
add(expr: Jdouble)
Returns: Jdouble
Public add
add(expr: Jint | Jchar | Jdouble)
Returns: Jint | Jdouble
Public sub
sub(expr: Jint | Jchar)
Returns: Jint
Public sub
sub(expr: Jdouble)
Returns: Jdouble
Public sub
sub(expr: Jint | Jchar | Jdouble)
Returns: Jint | Jdouble
Public mul
mul(expr: Jint | Jchar)
Returns: Jint
Public mul
mul(expr: Jdouble)
Returns: Jdouble
Public mul
mul(expr: Jint | Jchar | Jdouble)
Returns: Jint | Jdouble
Public div
div(expr: Jint | Jchar)
Returns: Jint
Public div
div(expr: Jdouble)
Returns: Jdouble
Public div
div(expr: Jint | Jchar | Jdouble)
Returns: Jint | Jdouble
Public mod
mod(expr: Jint | Jchar)
Returns: Jint
Public mod
mod(expr: Jdouble)
Returns: Jdouble
Public mod
mod(expr: Jint | Jchar | Jdouble)
Returns: Jint | Jdouble

Properties

Public value
value: number

Retrieve the actual value wrapped by this Jchar.

import {JArithmetic, JEquality, JRelational, JUnary} from '@j2se-js/java.lang.native.operator';
import {Jboolean, jboolean} from './jboolean_primitive';
import {Jdouble, jdouble} from './jdouble_primitive';
import {Jint, jint} from './jint_primitive'

/**
 * The char data type is a single 16-bit Unicode character.
 * It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).
 *
 * Note: To retrieve the actual numeric value wrapped in a Jchar you have to use <code>.value</code> syntax.
 */
export class Jchar implements JEquality<Jchar | Jint | Jdouble>,
                              JRelational<Jchar | Jint | Jdouble>,
                              JUnary<Jchar | Jint>,
                              JArithmetic<Jchar | Jint | Jdouble, Jint | Jdouble> {

  private static validate(value: string | Jint) {
    if (typeof value === 'string') {
      if (value.length > 1) {
        throw Error('incompatible types: string cannot be converted to char');
      }
    } else {
      if (value.value > 65535 || value.value < 0) {
        throw Error('incompatible types: possible lossy conversion from int to char');
      }
    }
  }

  private _value: string;

  public constructor(value: string | Jint) {
    Jchar.validate(value);

    if (typeof value === 'string') {
      this._value = value;
    } else {
      this._value = String.fromCharCode(value.value);
    }
  }

  /**
   * Retrieve the actual value wrapped by this Jchar.
   * @returns {string} value wrapped by this Jchar.
   */
  public get value(): number {
    return this._value.charCodeAt(0);
  }

  public toString(): string {
    return this._value;
  }

  // JEquality
  public eq(expr: Jchar | Jint | Jdouble): Jboolean {
    return jboolean((this.value === expr.value).toString());
  }

  public ne(expr: Jchar | Jint | Jdouble): Jboolean {
    return jboolean((this.value !== expr.value).toString());
  }

  // JRelational
  public lt(expr: Jchar | Jint | Jdouble): Jboolean {
    return jboolean((this.value < expr.value).toString());
  }

  public gt(expr: Jchar | Jint | Jdouble): Jboolean {
    return jboolean((this.value > expr.value).toString());
  }

  public le(expr: Jchar | Jint | Jdouble): Jboolean {
    return jboolean((this.value <= expr.value).toString());
  }

  public ge(expr: Jchar | Jint | Jdouble): Jboolean {
    return jboolean((this.value >= expr.value).toString());
  }

  // JUnary
  public plus(): Jint {
    return jint((+this.value).toString());
  }

  public inc(): Jchar {
    this._value = String.fromCharCode(this.value + 1);
    return jchar(this._value);
  }

  public dec(): Jchar {
    this._value = String.fromCharCode(this.value - 1);
    return jchar(this._value);
  }

  public minus(): Jint {
    return jint((-this.value).toString());
  }

  // JArithmetic
  public add(expr: Jchar | Jint): Jint;
  public add(expr: Jdouble): Jdouble;

  public add(expr: Jchar | Jint | Jdouble): Jint | Jdouble {
    if (expr instanceof Jdouble) {
      return jdouble((this.value + expr.value).toString());
    } else {
      return jint((this.value + expr.value).toString());
    }
  }

  public sub(expr: Jchar | Jint): Jint;
  public sub(expr: Jdouble): Jdouble;

  public sub(expr: Jchar | Jint | Jdouble): Jint | Jdouble {
    if (expr instanceof Jdouble) {
      return jdouble((this.value - expr.value).toString());
    } else {
      return jint((this.value - expr.value).toString());
    }
  }

  public mul(expr: Jchar | Jint): Jint;
  public mul(expr: Jdouble): Jdouble;

  public mul(expr: Jchar | Jint | Jdouble): Jint | Jdouble {
    if (expr instanceof Jdouble) {
      return jdouble((this.value * expr.value).toString());
    } else {
      return jint((this.value * expr.value).toString());
    }
  }

  public div(expr: Jchar | Jint): Jint;
  public div(expr: Jdouble): Jdouble;

  public div(expr: Jchar | Jint | Jdouble): Jint | Jdouble {
    if (expr instanceof Jdouble) {
      return jdouble((this.value / expr.value).toString());
    } else {
      return jint((this.value / expr.value).toString());
    }
  }

  public mod(expr: Jchar | Jint): Jint;
  public mod(expr: Jdouble): Jdouble;

  public mod(expr: Jchar | Jint | Jdouble): Jint | Jdouble {
    if (expr instanceof Jdouble) {
      return jdouble((this.value % expr.value).toString());
    } else {
      return jint((this.value % expr.value).toString());
    }
  }
}

/**
 * Factory for constructing a Jchar without use the new keyword
 * @param {number | string} value to be wrapped in the new Jchar.
 * @returns {Jchar} the Jchar created.
 */
export function jchar(value: string | Jint = jint('0')): Jchar {
  return new Jchar(value);
}

results matching ""

    No results matching ""