/*
 * Polygon.java
 *
 * Created on 8. februar 2007, 10:41
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.rometools.modules.georss.geometries;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Polygon, a surface object bounded by one external ring and zero or more internal rings
 *
 * @author runaas
 */
public final class Polygon extends AbstractSurface implements Cloneable {
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private AbstractRing exterior;
    private List<AbstractRing> interior;

    /** Creates a new instance of Polygon */
    public Polygon() {

    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        final Polygon retval = (Polygon) super.clone();
        if (exterior != null) {
            retval.exterior = (AbstractRing) exterior.clone();
        }
        if (interior != null) {
            retval.interior = new ArrayList<AbstractRing>();
            final Iterator<AbstractRing> it = interior.iterator();
            while (it.hasNext()) {
                final AbstractRing r = it.next();
                retval.interior.add((AbstractRing) r.clone());
            }
        }
        return retval;
    }

    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (!super.equals(obj)) {
            return false;
        }
        final Polygon pol = (Polygon) obj;
        if (exterior == null && pol.exterior == null) {
            ;
        } else if (exterior == null || pol.exterior == null) {
            return false;
        } else if (!exterior.equals(pol.exterior)) {
            return false;
        }

        // Not efficient.... (but the number of internal ringr is usually small).
        Iterator<AbstractRing> it = interior.iterator();
        while (it.hasNext()) {
            if (!pol.interior.contains(it.next())) {
                return false;
            }
        }
        it = pol.interior.iterator();
        while (it.hasNext()) {
            if (!interior.contains(it.next())) {
                return false;
            }
        }
        return true;
    }

    /**
     * Retrieve the outer border
     *
     * @return the border ring
     */
    public AbstractRing getExterior() {
        return exterior;
    }

    /**
     * Retrieve the inner border
     *
     * @return the list of border rings
     */
    public List<AbstractRing> getInterior() {
        if (interior == null) {
            interior = new ArrayList<AbstractRing>();
        }
        return interior;
    }

    /**
     * Set the outer border
     *
     * @param exterior the outer ring
     */
    public void setExterior(final AbstractRing exterior) {
        this.exterior = exterior;
    }

    /**
     * Set the list of inner borders (holes)
     *
     * @param interior the list of inner rings
     */
    public void setInterior(final List<AbstractRing> interior) {
        this.interior = interior;
    }
}
