Advertisement
Guest User

Decorator

a guest
Apr 1st, 2015
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.15 KB | None | 0 0
  1. import java.util.AbstractList;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4.  
  5.  
  6. public class OnlyNList extends AbstractList<String> {
  7.  
  8.     private List<String> decorated ;
  9.     private int n;
  10.  
  11.     public String get(int i) {
  12.         return decorated.get(i);
  13.     }
  14.  
  15.     public int size() {
  16.         return decorated.size();
  17.     }
  18.  
  19.     public OnlyNList(int n, List<String> decorated) {
  20.         this.n = n;
  21.         this.decorated = decorated;
  22.     }
  23.  
  24.     public String set(int i,String e) {
  25.         if (e.length() > this.n)
  26.             throw new UnsupportedOperationException();
  27.         return this.decorated.set(i,e);
  28.     }  
  29.  
  30.     public void add(int i,String e) {
  31.         if (e.length() > this.n)
  32.             throw new UnsupportedOperationException();
  33.         this.decorated.add(i,e);
  34.     }  
  35.  
  36.     public String remove(int i) {
  37.         return this.decorated.remove(i);
  38.     }
  39.  
  40.     public static void main(String argv[]) {
  41.         OnlyNList l = new OnlyNList(4, new ArrayList<String>());
  42.  
  43.         l.add("1234");
  44.  
  45.         System.out.println(l);
  46.  
  47.         try {
  48.             l.add("12345");
  49.         } catch (Exception e) {
  50.             System.out.println("OK");
  51.         }
  52.  
  53.         l.add("abcd");
  54.         l.add("1ab2");
  55.         l.add("xyz0");
  56.         System.out.println(l);
  57.  
  58.  
  59.         for(String s: l) System.out.println(s);
  60.     }  
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement