/*
 * Copyright 2020 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
 *
 *     https://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 example.app.books.model;

import java.util.UUID;

import org.springframework.util.Assert;

/**
 * The {@link ISBN} class is a Abstract Data Type (ADT) modeling either a {@link Book} ISBN-10 or ISBN-13 number.
 *
 * @author John Blum
 * @see java.lang.Comparable
 * @since 1.0.0
 */
@SuppressWarnings("unused")
public class ISBN implements Comparable<ISBN> {

	public static ISBN autoGenerated() {
		return of(UUID.randomUUID().toString());
	}

	public static ISBN of(String number) {

		Assert.hasText(number, String.format("Number [%s] is required", number));

		return new ISBN(number);
	}

	private String number;

	protected ISBN() { }

	private ISBN(String number) {
		this.number = number;
	}

	public String getNumber() {
		return this.number;
	}

	@Override
	public int compareTo(ISBN other) {
		return this.getNumber().compareTo(other.getNumber());
	}

	@Override
	public boolean equals(Object obj) {

		if (this == obj) {
			return true;
		}

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

		ISBN that = (ISBN) obj;

		return this.getNumber().equals(that.getNumber());
	}

	@Override
	public int hashCode() {

		int hashValue = 17;

		hashValue = 37 * hashValue + getNumber().hashCode();

		return hashValue;
	}

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