/*
	cursus - Race series management program
	Copyright 2011  Simon Arlott

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package eu.lp0.cursus.db.data;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import eu.lp0.cursus.util.Constants;

@Entity(name = "race_no")
@Table(uniqueConstraints = { @UniqueConstraint(columnNames = { "series_id", "organisation", "number" }) })
public class RaceNumber extends AbstractEntity implements Comparable<RaceNumber> {
	private String organisation;
	private Integer number;

	RaceNumber() {
	}

	public RaceNumber(Pilot pilot, String organisation, int number) {
		setSeries(pilot.getSeries());
		setPilot(pilot);
		setOrganisation(organisation);
		setNumber(number);
	}

	private Series series;

	@ManyToOne(optional = false)
	@JoinColumn(name = "series_id", nullable = false)
	public Series getSeries() {
		return series;
	}

	public void setSeries(Series series) {
		this.series = series;
	}

	@Column(nullable = false, length = Constants.MAX_STRING_LEN)
	public String getOrganisation() {
		return organisation.replaceAll("[0-9]", ""); //$NON-NLS-1$ //$NON-NLS-2$;
	}

	public void setOrganisation(String organisation) {
		this.organisation = organisation.replaceAll("[0-9]", ""); //$NON-NLS-1$ //$NON-NLS-2$
	}

	@Column(nullable = false)
	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}

	private Pilot pilot;

	@ManyToOne(optional = false)
	@JoinColumn(nullable = false)
	public Pilot getPilot() {
		return pilot;
	}

	public void setPilot(Pilot pilot) {
		this.pilot = pilot;
	}

	@Override
	public int compareTo(RaceNumber o) {
		int ret = getOrganisation().compareTo(o.getOrganisation());
		if (ret != 0) {
			return ret;
		}

		return ((Integer)getNumber()).compareTo(o.getNumber());
	}

	@Override
	public String toString() {
		return String.format("%s%02d", getOrganisation(), getNumber()); //$NON-NLS-1$
	}

	public static RaceNumber valueOfFor(String value, Pilot pilot) {
		Matcher matcher = Pattern.compile("^([^0-9]+)([0-9]+)$").matcher(value); //$NON-NLS-1$
		if (matcher.matches()) {
			String organisation = matcher.group(1);
			int number = Integer.valueOf(matcher.group(2));

			RaceNumber raceNumber = new RaceNumber(pilot, organisation, number);
			for (RaceNumber pilotRaceNumber : pilot.getRaceNumbers()) {
				if (pilotRaceNumber.compareTo(raceNumber) == 0) {
					raceNumber = pilotRaceNumber;
				}
			}
			return raceNumber;
		} else {
			throw new IllegalArgumentException("Unable to parse race number: " + value); //$NON-NLS-1$
		}
	}
}
@@ -1006,16 +1006,62 @@
 raint;%0A%0A
+import com.google.common.base.Preconditions;%0A%0A
 import e
@@ -1306,16 +1306,122 @@
 mber%3E %7B%0A
+%09public static final Pattern RACE_NUMBER_PATTERN = Pattern.compile(%22%5E(%5B%5E0-9%5D+)(%5B0-9%5D+)$%22); //$NON-NLS-1$%0A%0A
 %09private
@@ -2966,105 +2966,150 @@
 r = 
-Pattern.compile(%22%5E(%5B%5E0-9%5D+)(%5B0-9%5D+)$%22).matcher(value); //$NON-NLS-1$%0A%09%09if (matcher.matches()) %7B%0A%09
+RACE_NUMBER_PATTERN.matcher(value);%0A%09%09Preconditions.checkArgument(matcher.matches(), %22Unable to parse race number: %25s%22, value); //$NON-NLS-1$%0A
 %09%09St
@@ -3146,17 +3146,16 @@
 oup(1);%0A
-%09
 %09%09int nu
@@ -3199,17 +3199,16 @@
 2));%0A%0A%09%09
-%09
 RaceNumb
@@ -3268,17 +3268,16 @@
 umber);%0A
-%09
 %09%09for (R
@@ -3333,17 +3333,16 @@
 )) %7B%0A%09%09%09
-%09
 if (pilo
@@ -3387,17 +3387,16 @@
 ) %7B%0A%09%09%09%09
-%09
 raceNumb
@@ -3424,19 +3424,16 @@
 %0A%09%09%09
-%09
 %7D%0A
-%09
 %09%09%7D%0A%09%09
-%09
 retu
@@ -3451,117 +3451,8 @@
 er;%0A
-%09%09%7D else %7B%0A%09%09%09throw new IllegalArgumentException(%22Unable to parse race number: %22 + value); //$NON-NLS-1$%0A%09%09%7D%0A
 %09%7D%0A%7D

use Preconditions