//
// AbstractData.java
//

/*
ImageJ software for multidimensional image processing and analysis.

Copyright (c) 2010, ImageJDev.org.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the names of the ImageJDev.org developers nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/

package imagej.data;

import imagej.ImageJ;
import imagej.data.event.DataCreatedEvent;
import imagej.data.event.DataDeletedEvent;
import imagej.data.roi.Overlay;
import imagej.event.EventService;
import net.imglib2.meta.AxisType;

/**
 * Base implementation of {@link Data}.
 * 
 * @author Curtis Rueden
 * @author Barry DeZonia
 * @see Dataset
 * @see Overlay
 */
public abstract class AbstractData implements Data, Comparable<Data> {

	private int refs = 0;
	
	private String name;

	final protected EventService eventService;

	public AbstractData() {
		eventService = ImageJ.get(EventService.class);
	}

	// -- AbstractData methods --
	
	/**
	 * Informs interested parties that the data object has become relevant and
	 * should be registered. Called the first time the reference count is
	 * incremented. Classes that extend this class may choose to override this
	 * method to publish more specific events.
	 */
	protected void register() {
		eventService.publish(new DataCreatedEvent(this));
	}

	/**
	 * Informs interested parties that the data object is no longer relevant and
	 * should be deleted. Called when the reference count is decremented to zero.
	 * Classes that extend this class may choose to override this method to
	 * publish more specific events.
	 * */
	protected void delete() {
		eventService.publish(new DataDeletedEvent(this));
	}

	// -- Object methods --
	
	@Override
	public String toString() {
		return getName();
	}

	// -- Data methods --
	
	@Override
	public void incrementReferences() {
		refs++;
		if (refs == 1) register();
	}

	@Override
	public void decrementReferences() {
		if (refs == 0)
			throw new IllegalArgumentException(
				"decrementing reference count when it is already 0");
		refs--;
		if (refs == 0) delete();
	}

	// -- LabeledSpace methods --
	
	@Override
	public long[] getDims() {
		final long[] dims = new long[numDimensions()];
		getExtents().dimensions(dims);
		return dims;
	}

	@Override
	public AxisType[] getAxes() {
		final AxisType[] axes = new AxisType[numDimensions()];
		axes(axes);
		return axes;
	}

	// -- Named methods --
	
	@Override
	public String getName() {
		return name;
	}

	@Override
	public void setName(String name) {
		this.name = name;
	}

	// -- Comparable methods --
	
	@Override
	public int compareTo(final Data data) {
		return getName().compareTo(data.getName());
	}

}

@@ -2267,18 +2267,17 @@
 hods --%0A
-%09
 %0A
+
 %09/**%0A%09 *
@@ -2900,18 +2900,16 @@
 vents.%0A%09
- *
  */%0A%09pro
@@ -3004,34 +3004,33 @@
 ject methods --%0A
-%09
 %0A
+
 %09@Override%0A%09publ
@@ -3091,34 +3091,33 @@
 Data methods --%0A
-%09
 %0A
+
 %09@Override%0A%09publ
@@ -3255,16 +3255,18 @@
 fs == 0)
+ %7B
 %0A%09%09%09thro
@@ -3282,16 +3282,13 @@
 egal
-Argument
+State
 Exce
@@ -3352,16 +3352,20 @@
 dy 0%22);%0A
+%09%09%7D%0A
 %09%09refs--
@@ -3420,34 +3420,33 @@
 pace methods --%0A
-%09
 %0A
+
 %09@Override%0A%09publ
@@ -3716,34 +3716,33 @@
 amed methods --%0A
-%09
 %0A
+
 %09@Override%0A%09publ
@@ -3814,16 +3814,22 @@
 setName(
+final 
 String n
@@ -3888,17 +3888,16 @@
 hods --%0A
-%09
 %0A%09@Overr

Fix style