package com.github.pkunk.progressquest.util;

/**
 * User: pkunk
 * Date: 2012-01-02
 */
public final class Roman {
    private final int integer;
    private final String roman;

    public Roman(int value) {
        this.roman = toRoman(value);
        this.integer = value;
    }

    public int getInt() {
        return integer;
    }

    public String getRoman() {
        return roman;
    }

    @Override
    public int hashCode() {
        return integer;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Roman) {
            return integer == ((Roman)obj).integer;
        }
        return false;
    }

    @Override
    public String toString() {
        return roman;
    }

    private static final char[] R = {'ↂ', 'ↁ', 'M', 'D', 'C', 'L', 'X', 'V', 'I'};

    private static final int MAX = 10000; // value of R[0], must be a power of 10

    private static final int[][] DIGITS = {
            {},{0},{0,0},{0,0,0},{0,1},{1},
            {1,0},{1,0,0},{1,0,0,0},{0,2}};

    public static String toRoman(int value) {
        if (value < 0 || value >= MAX*4) throw new IllegalArgumentException(
                "toRoman: " + value + " is not between 0 and " + (MAX*4-1));
        if (value == 0) return "N";
        StringBuilder sb = new StringBuilder();
        for (int i = 0, m = MAX; m > 0; m /= 10, i += 2) {
            int[] d = DIGITS[(value/m)%10];
            for (int n: d) sb.append(R[i-n]);
        }
        return sb.toString();
    }
}

@@ -763,18 +763,8 @@
  = %7B
-'%E2%86%82', '%E2%86%81', 
 'M',
@@ -835,17 +835,16 @@
 X = 1000
-0
 ; // val
@@ -1082,27 +1082,23 @@
  %3C 0
- %7C%7C value %3E= MAX*4)
+) %7B%0A           
  thr
@@ -1133,85 +1133,67 @@
 ion(
-%0A                %22toRoman: %22 + value + %22 is not between 0 and %22 + (MAX*4-1));
+value + %22is wrong - Romans are always positive%22);%0A        %7D
 %0A   
@@ -1212,16 +1212,30 @@
 ue == 0)
+ %7B%0A           
  return 
@@ -1239,16 +1239,35 @@
 rn %22N%22;%0A
+        %7D%0A        %0A
         
@@ -1302,24 +1302,137 @@
 gBuilder();%0A
+        %0A        while (value %3E MAX) %7B%0A            value -= MAX;%0A            sb.append(R%5B0%5D);%0A        %7D%0A        %0A
         for 

remove upper bound for romans