package se.ericthelin.fractions;

import java.util.regex.Pattern;

import static java.util.Objects.requireNonNull;

public class Fraction {

	private static final Pattern FRACTION_PATTERN = Pattern.compile("\\d+/\\d+");
	private static final Pattern INTEGER_PATTERN = Pattern.compile("\\d+");

	public static Fraction of(String text) {
		if (!FRACTION_PATTERN.matcher(text).matches()
				&& !INTEGER_PATTERN.matcher(text).matches()) {
			throw new InvalidFractionFormatException(text);
		}

		if (text.endsWith("/0")) {
			throw new ZeroDenominatorException(text);
		}

		return new Fraction(text);
	}

	private final String text;

	private Fraction(String text) {
		this.text = requireNonNull(text);
	}

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

	@Override
	public boolean equals(Object obj) {
		if (obj == this) {
			return true;
		}

		if (!(obj instanceof Fraction)) {
			return false;
		}

		Fraction other = (Fraction) obj;

		return text.equals(other.text);
	}

	public Fraction plus(Fraction term) {
		throw new UnsupportedOperationException("Not yet implemented");
	}
}

@@ -27,16 +27,48 @@
 tions;%0A%0A
+import java.util.regex.Matcher;%0A
 import j
@@ -115,35 +115,29 @@
 ava.
-util.Objects.requireNonNull
+lang.Integer.parseInt
 ;%0A%0Ap
@@ -230,17 +230,21 @@
 le(%22
+(
 %5C%5Cd+
-/
+)/(
 %5C%5Cd+
+)
 %22);%0A
@@ -361,21 +361,42 @@
 xt) %7B%0A%09%09
-if (!
+Matcher fractionMatcher = 
 FRACTION
@@ -409,32 +409,56 @@
 RN.matcher(text)
+;%0A%0A%09%09if (fractionMatcher
 .matches()%0A%09%09%09%09&
@@ -455,61 +455,282 @@
 es()
+) %7B
 %0A%09%09%09
-%09&& !INTEGER_PATTERN.matcher(text).matches()) %7B%0A%09
+return Fraction.of(%0A%09%09%09%09%09parseInt(fractionMatcher.group(1)),%0A%09%09%09%09%09parseInt(fractionMatcher.group(2)));%0A%09%09%7D%0A%0A%09%09Matcher integerMatcher = INTEGER_PATTERN.matcher(text);%0A%0A%09%09if (integerMatcher.matches()) %7B%0A%09%09%09return Fraction.of(parseInt(integerMatcher.group(0)), 1);%0A%09%09%7D%0A%0A
 %09%09th
@@ -779,91 +779,73 @@
 t);%0A
-%09
 %09%7D%0A%0A%09
-%09if (text.endsWith(%22/0%22)) %7B%0A%09%09%09throw new ZeroDenominatorException(text);%0A%09%09%7D%0A
+private static Fraction of(int numerator, int denominator) %7B
 %0A%09%09r
@@ -859,28 +859,46 @@
 ew Fraction(
-text
+numerator, denominator
 );%0A%09%7D%0A%0A%09priv
@@ -905,96 +905,246 @@
 ate 
-f
 in
-al String text;%0A%0A%09private Fraction(String text) %7B%0A%09%09this.text = requireNonNull(text);
+t numerator;%0A%09private int denominator;%0A%0A%09public Fraction(int numerator, int denominator) %7B%0A%09%09this.numerator = numerator;%0A%09%09this.denominator = denominator;%0A%0A%09%09if (denominator == 0) %7B%0A%09%09%09throw new ZeroDenominatorException(toString());%0A%09%09%7D
 %0A%09%7D%0A
@@ -1192,20 +1192,45 @@
 %09return 
-text
+numerator + %22/%22 + denominator
 ;%0A%09%7D%0A%0A%09@
@@ -1424,31 +1424,76 @@
 urn 
-text.equals(other.text)
+numerator == other.numerator%0A%09%09%09%09&& denominator == other.denominator
 ;%0A%09%7D

Change Fraction implementation from text to integers