package com.atreceno.it.javanese.concurrency;

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

/**
 * Requirements: 1. Given a list of integers, remove the elements of the list
 * below a threshold. 2. Null and thread safe.
 * 
 * @author atreceno
 * 
 */
public class Threshold {

	private List<Integer> list;
	private int threshold;

	public Threshold(int threshold, List<Integer> list) {
		if (list == null)
			throw new IllegalArgumentException("list is null");
		this.list = list;
		this.threshold = threshold;
	}

	public void truncate() {
		for (Iterator<Integer> it = list.iterator(); it.hasNext();) {
			if (it.next() < this.threshold) {
				it.remove();
			}
		}
	}

	public void audit() {
		for (Integer value : this.list)
			if (value < this.threshold)
				System.out.println("ALERT!");
	}

	public void print() {
		System.out.println(this.list);
		System.out.println(this.threshold);
	}

	public static void main(String[] args) {

		/*
		 * new Threshold(3, Arrays.asList(2, 5, 2, 1, 7)) will throw an
		 * UnsupportedOperationException
		 */

		// Initialize the list
		List<Integer> myList = new ArrayList<Integer>(Arrays.asList(2, 5, 2, 1,
				7, -2, 0, 8));
		for (int i = 0; i < 1000; i++) {
			myList.add(new Random().nextInt(100));
		}

		Threshold t = new Threshold(90, myList);

		// N truncate threads
		for (int i = 0; i < 1; i++) {
			t.truncateThread(i);
		}

		// N audit threads
		for (int i = 0; i < 1; i++) {
			t.auditThread(i);
		}

	}

	private void truncateThread(int i) {
		Thread t = new Thread(new Runnable() {

			@Override
			public void run() {
				while (!Thread.interrupted()) {
					truncate();
				}
			}

		}, "truncate-" + i);
		t.start();
	}

	private void auditThread(int i) {
		Thread t = new Thread(new Runnable() {

			@Override
			public void run() {
				while (!Thread.interrupted()) {
					audit();
				}
			}
		}, "audit-" + i);
		t.start();
	}

}

@@ -93,16 +93,46 @@
 Arrays;%0A
+import java.util.Collections;%0A
 import j
@@ -646,24 +646,159 @@
 runcate() %7B%0A
+%09%09// for(int i = list.size() - 1; i %3E 0; i--) %7B%0A%09%09// if (list.get(i) %3C threshold)%0A%09%09// list.remove(i);%0A%09%09// %7D%0A%09%09synchronized (list) %7B%0A%09
 %09%09for (Itera
@@ -848,24 +848,25 @@
 xt();) %7B%0A%09%09%09
+%09
 if (it.next(
@@ -869,21 +869,16 @@
 ext() %3C 
-this.
 threshol
@@ -878,24 +878,25 @@
 hreshold) %7B%0A
+%09
 %09%09%09%09it.remov
@@ -900,16 +900,22 @@
 move();%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D
@@ -942,16 +942,160 @@
 dit() %7B%0A
+%09%09// for(int i = 0; i%3C list.size(); i++) %7B%0A%09%09// if (list.get(i) %3C threshold)%0A%09%09// System.out.println(%22ALERT!%22);%0A%09%09// %7D%0A%09%09synchronized (list) %7B%0A%09
 %09%09for (I
@@ -1113,19 +1113,15 @@
 e : 
-this.
 list)%0A
+%09
 %09%09%09i
@@ -1131,21 +1131,16 @@
 value %3C 
-this.
 threshol
@@ -1142,16 +1142,17 @@
 eshold)%0A
+%09
 %09%09%09%09Syst
@@ -1177,16 +1177,20 @@
 ERT!%22);%0A
+%09%09%7D%0A
 %09%7D%0A%0A%09pub
@@ -1229,21 +1229,16 @@
 println(
-this.
 list);%0A%09
@@ -1257,21 +1257,16 @@
 println(
-this.
 threshol
@@ -1320,121 +1320,8 @@
  %7B%0A%0A
-%09%09/*%0A%09%09 * new Threshold(3, Arrays.asList(2, 5, 2, 1, 7)) will throw an%0A%09%09 * UnsupportedOperationException%0A%09%09 */%0A%0A
 %09%09//
@@ -1362,24 +1362,58 @@
 r%3E myList = 
+Collections%0A%09%09%09%09.synchronizedList(
 new ArrayLis
@@ -1441,37 +1441,28 @@
 ist(
-2, 5
+7
 , 
+-
 2,
- 1,
 %0A%09%09%09%09
-7, -2, 
+%09%09
 0, 8))
+)
 ;%0A%09%09
@@ -1484,17 +1484,16 @@
  i %3C 100
-0
 ; i++) %7B
@@ -1540,16 +1540,86 @@
 );%0A%09%09%7D%0A%0A
+%09%09// Construct the class (Careful with UnsupportedOperationException)%0A
 %09%09Thresh
@@ -1692,33 +1692,33 @@
 (int i = 0; i %3C 
-1
+4
 ; i++) %7B%0A%09%09%09t.tr
@@ -1782,17 +1782,17 @@
  0; i %3C 
-1
+4
 ; i++) %7B

Make Threshold thread-safe