0

import java.util.AbstractList;

public class ItemSet extends AbstractList {

private Item[] arr;
private ItemClass itemClass;

public ItemSet(Item item) {
arr = new Item[1];
arr[0] = item;
}

/*
 * (non-Javadoc)
 * 
 * @see java.util.AbstractList#add(java.lang.Object)
 */
@Override
public boolean add(Item e) {
boolean isNotAdded = true;
for (int i = 0; i < arr.length; i++) {
    if (e.getRule().compareTo(arr[i].getRule())) {
    if (e.getDot() == arr[i].getDot()) {
        isNotAdded = false;
        break;
    }
    }
}
if (isNotAdded) {
    Item[] oldArr = arr;
    arr = new Item[oldArr.length + 1];
    System.arraycopy(oldArr, 0, arr, 0, oldArr.length);
    arr[oldArr.length] = e;
}
return isNotAdded;
}

@Override
public Item get(int index) {
return arr[index];
}

@Override
public int size() {
return arr.length;
}

// SETTER

/**
 * @param itemClass
 *            the itemClass to set
 */
public void setItemClass(ItemClass itemClass) {
this.itemClass = itemClass;
}

//

// GETTER

/**
 * @return the itemClass
 */
public ItemClass getItemClass() {
return itemClass;
}
//

}

How to cover for instance removes methods? How do not use an own list implementation?

itun
  • 3,439
  • 12
  • 51
  • 75

2 Answers2

3

If you don't want to implement the other methods in the interface, simply define them to throw an exception:

public bool remove(Object o)
{
    throw new UnsupportedOperationException();
}

If you don't want to even do that, you should inherit from a class, not implement an interface.

Platinum Azure
  • 45,269
  • 12
  • 110
  • 134
-1

If you don't want to inherit remove(), then don't extend AbstractList, instead delegate the methods you want to an ArrayList.

Like this (and for the love of god don't comment your setters and getters)

    public class ItemSet {
    private final ArrayList<Item> items = new ArrayList<Item>();
    private ItemClass itemClass;

    public ItemSet(Item item) {
        items.add(item);
    }

    public boolean add(Item e) {
        boolean isNotAdded = true;
        for (Item item : items)
            if (e.getRule().compareTo(item.getRule()))
                if (e.getDot() == item.getDot()) {
                    isNotAdded = false;
                    break;
                }
        // why are you adding here?
        if (isNotAdded)
            items.add(e);
        return isNotAdded;
    }

    public Item get(int index) {
        return items.get(index);
    }

    public int size() {
        return items.size();
    }

    public void setItemClass(ItemClass itemClass) {
        this.itemClass = itemClass;
    }

    public ItemClass getItemClass() {
        return itemClass;
    }
}
}
Garrett Hall
  • 29,524
  • 10
  • 61
  • 76
  • Is it a good style of the java programming? I think this is a burden to the call stack, isn' t it? – itun Sep 14 '11 at 12:48
  • Calling add() in ArrayList runs in constant time. In your implementation System.arraycopy() runs in O(n) time. These things make a much bigger difference than possibly increasing the call stack which may be optimized away(?). A thinner public interface is better design as well. – Garrett Hall Sep 14 '11 at 13:28
  • Calling add() in ArrayList runs in constant time. In your implementation System.arraycopy() runs in O(n) time. I know this, I had to write in the fast way, so I don't care about this. But thanks. A thinner public interface is better design as well. What do you mean? – itun Sep 14 '11 at 13:42
  • A thin interface (less exposed methods) reduces [coupling](http://en.wikipedia.org/wiki/Coupling_%28computer_science%29). – Garrett Hall Sep 14 '11 at 14:06