package com.lekebilen.quasseldroid;

import java.util.Collection;
import java.util.Collections;
import java.util.Observable;
import java.util.Observer;
import java.util.SortedMap;
import java.util.TreeMap;

public class BufferCollection extends Observable implements Observer {
	
	private SortedMap<Integer, Buffer> buffers = new TreeMap<Integer, Buffer>();
	
	private static final String TAG = BufferCollection.class.getSimpleName();
	
	public BufferCollection() {
		
	}
	
	public void addBuffer(Buffer buffer) {
		buffers.put(buffer.getInfo().id, buffer);
		this.setChanged();
		buffer.addObserver(this);
		notifyObservers();
	}
	
	public int getBufferCount() {
		return buffers.size();
	}
	
	public Buffer getPos(int pos) {
		//TODO: might want to make something better then this. For when we want to change possitions and crap we need to be able to move stuff around
		return (Buffer) buffers.values().toArray()[pos];
	}
	
	public Buffer getBuffer(int bufferId) {
		return this.buffers.get(bufferId);
		
	}

	public boolean hasBuffer(int id) {
		return buffers.containsKey(id);
	}
	
	public void addBuffers(Collection<Buffer> buffers) {
		for (Buffer buffer: buffers) {
			this.buffers.put(buffer.getInfo().id, buffer);
			buffer.addObserver(this);
		}
		this.setChanged();
		notifyObservers();
	}
	

	@Override
	public void update(Observable arg0, Object arg1) {
		this.setChanged();
		notifyObservers();
		
	}
}

@@ -30,16 +30,44 @@
 droid;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -117,16 +117,65 @@
 ctions;%0A
+import java.util.HashMap;%0Aimport java.util.List;%0A
 import j
@@ -225,62 +225,8 @@
 ver;
-%0Aimport java.util.SortedMap;%0Aimport java.util.TreeMap;
 %0A%0Apu
@@ -309,14 +309,12 @@
 ate 
-Sorted
+Hash
 Map%3C
@@ -348,12 +348,12 @@
 new 
-Tree
+Hash
 Map%3C
@@ -368,24 +368,84 @@
  Buffer%3E();%0A
+%09private List%3CBuffer%3E bufferList = new ArrayList%3CBuffer%3E();%0A
 %09%0A%09private s
@@ -632,16 +632,74 @@
 uffer);%0A
+%09%09bufferList.add(buffer);%0A%09%09Collections.sort(bufferList);%0A
 %09%09this.s
@@ -1031,33 +1031,21 @@
 ffer
-s.values().toArray()%5Bpos%5D
+List.get(pos)
 ;%0A%09%7D
@@ -1344,16 +1344,43 @@
 uffer);%0A
+%09%09%09bufferList.add(buffer);%0A
 %09%09%09buffe
@@ -1404,16 +1404,48 @@
 s);%0A%09%09%7D%0A
+%09%09Collections.sort(bufferList);%0A
 %09%09this.s

fix sorting of buffers, in the worst possible way :D