/*
 * Copyright 2009-2010 Yuichiro Moriguchi
 *
 * 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 net.morilib.math.order;

import java.util.Collections;
import java.util.Set;

import net.morilib.util.IntMath;

/**
 *
 *
 * @author MORIGUCHI, Yuichiro 2012/03/11
 */
class PolynomialOrder extends OrderElement {

	//
	int order;

	//
	PolynomialOrder(int order) {
		this.order = order;
	}

	/* (non-Javadoc)
	 * @see net.morilib.math.order.AbstractOrderNotation#level()
	 */
	public int level() {
		return POLY_LV;
	}

	/* (non-Javadoc)
	 * @see net.morilib.math.order.OrderNotation#multiply(net.morilib.math.order.OrderNotation)
	 */
	public OrderElement multiply(OrderElement o) {
		return new PolynomialOrder(order + ((PolynomialOrder)o).order);
	}

//	/* (non-Javadoc)
//	 * @see net.morilib.math.order.OrderNotation#divide(net.morilib.math.order.OrderNotation)
//	 */
//	public OrderElement divide(OrderElement o) {
//		return new PolynomialOrder(order - ((PolynomialOrder)o).order);
//	}
//
//	@Override
//	public OrderElement invert() {
//		return new PolynomialOrder(-order);
//	}

	@Override
	public Set<OrderElement> log() {
		if(order == 0)  throw new ArithmeticException();
		return Collections.<OrderElement>singleton(
				new LogarithmicOrder(1, 1));
	}

	/* (non-Javadoc)
	 * @see net.morilib.math.order.OrderElement#isSameLevel(net.morilib.math.order.OrderElement)
	 */
	@Override
	public int compareLevel(OrderElement b) {
		return IntMath.compareTo(level(), b.level());
	}

	/* (non-Javadoc)
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	public int compareTo(OrderElement o) {
		if(level() < o.level()) {
			return -1;
		} else if(level() > o.level()) {
			return 1;
		} else if(order < ((PolynomialOrder)o).order) {
			return -1;
		} else if(order > ((PolynomialOrder)o).order) {
			return 1;
		} else {
			return 0;
		}
	}

	@Override
	public boolean isConstant() {
		return order == 0;
	}

	public String toString() {
		switch(order) {
		case 0:   return "1";
		case 1:   return "n";
		default:  return "n^" + order;
		}
	}

}
