package androidStringResources;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * This class represent the quantity string element in the Android's strings.xml file. In the strings.xml, it is like:
 * <br/>
 * {@code
 * 		<plurals name="plural_name">
 * 			<item quantity=["zero" | "one" | "two" | "few" | "many" | "other"]>text_string</item>
 * 		</plurals>
 * }
 */
public class AndroidQuantityString extends AndroidStringItemsBase {
    
	/**
	 * @param n
	 * 		Refer to the AndroidStringItemsBase class
	 * @param items
	 * 		The arrayList storing the instances of AndroidQuantityString.AndroidQuantityItem.
	 */
    public AndroidQuantityString(String n, ArrayList<AndroidQuantityItem> items) {  
        super(n);  
        
        // Retain items with valid quantity
        ArrayList<AndroidQuantityItem> valids = new  ArrayList<AndroidQuantityItem>();
        for (AndroidQuantityItem i : items) {
            if (AndroidQuantityString.isValidQuantity(i.quantity)) {
            	valids.add(i);
            }
        }        
        this.setItems(valids);
    }
    
    
    /*
     * Properties
     **********/
    
    /**
     * The valid quantity values.
     */
    private static final String[] validQuantities = {
        "zero", "one", "two", "few", "many", "other"
    };
    
    
    /*
     * Methods
     **********/
        
    /**
     * Check the validity of input quantity value.
     * @param quantity
     * 		The quantity value.
     * @return
     * 		- If valid: true
     * 		<br/>
     * 		- If invalid: false
     */
    public static boolean isValidQuantity(String quantity) {
        return (Arrays.asList(AndroidQuantityString.validQuantities).indexOf(quantity) >= 0);
    }
    
    /**
     * @return
     * 		- If there are items: the next instance of AndroidQuantityString.AndroidQuantityItem.
     * 		<br/>
     * 		- If no items: null
     */
    @Override
    public AndroidQuantityItem nextItem() {          
        if (this.itemsItr.hasNext()) {
            AndroidQuantityItem it = (AndroidQuantityItem) this.itemsItr.next();
            return new AndroidQuantityItem(it.quantity, it.value);
        } else {
            return null;
        }
    }
    
    
    /*
     * Classes
     **********/   
    
    /**
     * This class represent the item node in the quantity string element. In the strings.xml, it is like:
	 * <br/>
	 * {@code
	 * 		<item quantity=["zero" | "one" | "two" | "few" | "many" | "other"]>text_string</item>
	 * }
     */
    public static class AndroidQuantityItem {
        
    	/**
    	 * @param q
    	 * 		Refer to this.quantity.
    	 * @param v
    	 * 		Refer to this.value.
    	 */
        public AndroidQuantityItem(String q, String v) {
            this.quantity = q;
            this.value = v;
        }
        
        /**
         * The value of the item node's quantity attribute.
         */
        public String quantity;
        
        /**
         * The string value of the item node
         */
        public String value;
    }
}

@@ -85,16 +85,44 @@
 ays;%0D%0A%0D%0A
+import libs.MyException;%0D%0A%0D%0A
 /**%0D%0A * 
@@ -965,24 +965,41 @@
 : items) %7B%0D%0A
+        %09try %7B%0D%0A%09
             
@@ -1052,24 +1052,25 @@
 antity)) %7B%0D%0A
+%09
             
@@ -1086,28 +1086,325 @@
 dd(i);%0D%0A
-            
+%09            %7D else %7B%0D%0A%09            %09throw new MyException(%0D%0A%09            %09%09String.format(%22The item(value = %25s) in the quantity string(name = %25s) has invalid quantity(= %25s)%22, i.value, n, i.quantity)%0D%0A%09            %09);%0D%0A%09            %7D%0D%0A        %09%7D catch (MyException e) %7B%0D%0A        %09%09e.print1stPoint();%0D%0A        %09
 %7D%0D%0A     

Update AndroidQuantityString.java for adding the exception warning.