/*
 * Copyright 2007-2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.fluxchess.jcpi.models;

public enum GenericChessman {

  PAWN('P'),
  KNIGHT('N'),
  BISHOP('B'),
  ROOK('R'),
  QUEEN('Q'),
  KING('K');

  public static final GenericChessman[] promotions = new GenericChessman[]{KNIGHT, BISHOP, ROOK, QUEEN};

  public static final GenericChessman[] sliders = new GenericChessman[]{BISHOP, ROOK, QUEEN};

  private final char token;

  private GenericChessman(char token) {
    this.token = token;
  }

  private static GenericChessman _valueOf(char token) {
    for (GenericChessman chessman : values()) {
      if (Character.toLowerCase(token) == Character.toLowerCase(chessman.token)) {
        return chessman;
      }
    }

    return null;
  }

  public static GenericChessman valueOf(char token) {
    GenericChessman chessman = _valueOf(token);
    if (chessman != null) {
      return chessman;
    } else {
      throw new IllegalArgumentException();
    }
  }

  public static boolean isValid(char token) {
    return _valueOf(token) != null;
  }

  private static GenericChessman _valueOfPromotion(char token) {
    for (GenericChessman chessman : promotions) {
      if (Character.toLowerCase(token) == Character.toLowerCase(chessman.token)) {
        return chessman;
      }
    }

    return null;
  }

  public static GenericChessman valueOfPromotion(char token) {
    GenericChessman chessman = _valueOfPromotion(token);
    if (chessman != null) {
      return chessman;
    } else {
      throw new IllegalArgumentException();
    }
  }

  public static boolean isValidPromotion(char token) {
    return _valueOfPromotion(token) != null;
  }

  public boolean isLegalPromotion() {
    for (GenericChessman chessman : promotions) {
      if (this == chessman) {
        return true;
      }
    }

    return false;
  }

  public boolean isSliding() {
    for (GenericChessman chessman : sliders) {
      if (this == chessman) {
        return true;
      }
    }

    return false;
  }

  public char toCharAlgebraic() {
    if (this == PAWN) {
      throw new UnsupportedOperationException();
    }

    return this.token;
  }

  public char toChar(GenericColor color) {
    if (color == null) throw new IllegalArgumentException();

    return color.transform(this.token);
  }

}

@@ -649,16 +649,43 @@
 odels;%0A%0A
+import java.util.EnumSet;%0A%0A
 public e
@@ -805,32 +805,40 @@
 ic static final 
+EnumSet%3C
 GenericChessman%5B
@@ -836,18 +836,17 @@
 Chessman
-%5B%5D
+%3E
  promoti
@@ -851,38 +851,27 @@
 tions = 
-new GenericChessman%5B%5D%7B
+EnumSet.of(
 KNIGHT, 
@@ -885,27 +885,26 @@
  ROOK, QUEEN
-%7D;%0A
+);
 %0A  public st
@@ -914,16 +914,24 @@
 c final 
+EnumSet%3C
 GenericC
@@ -937,18 +937,17 @@
 Chessman
-%5B%5D
+%3E
  sliders
@@ -953,30 +953,19 @@
 s = 
-new GenericChessman%5B%5D%7B
+EnumSet.of(
 BISH
@@ -979,17 +979,17 @@
 K, QUEEN
-%7D
+)
 ;%0A%0A  pri
@@ -2288,303 +2288,111 @@
     
-for (GenericChessman chessman : promotions) %7B%0A      if (this == chessman) %7B%0A        return true;%0A      %7D%0A    %7D%0A%0A    return false;%0A  %7D%0A%0A  public boolean isSliding() %7B%0A    for (GenericChessman chessman : sliders) %7B%0A      if (this == chessman) %7B%0A        return true;%0A      %7D%0A    %7D%0A%0A    return false
+return promotions.contains(this);%0A  %7D%0A%0A  public boolean isSliding() %7B%0A    return sliders.contains(this)
 ;%0A  

Switch to EnumSet