/**
 * @license
 * Copyright 2017 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package foam.lib.json;

import foam.lib.parse.*;

public class ByteParser implements Parser {
  
  private final static Parser instance__ = new ByteParser();

  public static Parser instance() { return instance__; }
  
  public PStream parse(PStream ps, ParserContext x) {
    long n = 0;

    boolean negate = false;

    if ( ! ps.valid() ) return null;

    char c = ps.head();

    if ( c == '-' ) {
      negate = true;
      ps = ps.tail();
      if ( ! ps.valid() ) return null;
      c = ps.head();
    }

    if ( Character.isDigit(c) ) n = Character.digit(c, 10);
    else return null;

    ps = ps.tail();

    while ( ps.valid() ) {
      c = ps.head();
      if ( Character.isDigit(c) ) {
        n *= 10;
        n += Character.digit(c, 10);
        if (n > Byte.MAX_VALUE) {
            return null;
        }
      } else {
        break;
      }
      ps = ps.tail();
    }

    if ( negate ) n *= -1;

    return ps.setValue((byte)n);
  }
}

/**
 * @license
 * Copyright 2017 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package foam.lib.json;

import foam.lib.parse.*;

public class ByteParser implements Parser {
  
  private final static Parser instance__ = new ByteParser();

  public static Parser instance() { return instance__; }
  
  private ByteParser() {}
  
  public PStream parse(PStream ps, ParserContext x) {
    long n = 0;

    boolean negate = false;

    if ( ! ps.valid() ) return null;

    char c = ps.head();

    if ( c == '-' ) {
      negate = true;
      ps = ps.tail();
      if ( ! ps.valid() ) return null;
      c = ps.head();
    }

    if ( Character.isDigit(c) ) n = Character.digit(c, 10);
    else return null;

    ps = ps.tail();

    while ( ps.valid() ) {
      c = ps.head();
      if ( Character.isDigit(c) ) {
        n *= 10;
        n += Character.digit(c, 10);
        if (n > Byte.MAX_VALUE) {
            return null;
        }
      } else {
        break;
      }
      ps = ps.tail();
    }

    if ( negate ) n *= -1;

    return ps.setValue((byte)n);
  }
}

create a private constructor.