Radical Refactoring 
by Eugene Belyaev,  Maxim Shafirov, and Ann Oreshnikova

Listing One
public class UserManagement {
   private static UserManagement ourSingleInstance;
   private AccessRightsTable myRightsTable;
   private UserManagement() {
      myRightsTable = new AccessRightsTable();
   }
   public static UserManagement getInstance() {
      if (ourSingleInstance == null) {
         ourSingleInstance = new UserManagement();
      }
      return ourSingleInstance;
   }
   public AccessRightsTable getRightsTable() {
      return myRightsTable;
   }
}


Listing Two
public class AccessRightsTable {
    private Map myUserToRightsSet;
    public AccessRigtsTable() {
        myUserToRightsSet = newHashMap();
    }
    public void grantRight(User user, Right right){
        Set rightsSet = (Set) myUserToRightsSet.get(user);
        if(rightsSet == null) {
            rightsSet = new HashSet ();
            myUserToRightsSet.put(user, rightsSet);
        }
        rightsSet.add(right);
    }
    public void revokeRight(User user, Right right) {...}
    public Boolean checkRight(User user, Right right) {...}
    public User[] getAllUsers() {...}
    public Right[] getUserRights(User user) {...}
}


Listing Three
public class Client {
   /** This method revokes CREATE_NEW_ACCOUNT right from all users
    * currently registered in rights table.
    */
   public static void revokeCreateNewAccount() {
      AccessRightsTable rightsTable =
                          UserManagement.getInstance().getRightsTable();
      User[] allUsers = rightsTable.getAllUsers();
      for (int i = 0; i < allUsers.length; i++) {
         User user = allUsers[i];
         rightsTable.revokeRight(user, Right.CREATE_NEW_ACCOUNT);
      }
   }
   /** This method creates new AccessRightsTable copy. This table is not
    * intended to be modified. @return new copy table. NOT TO BE MODIFIED!
    */
   public static AccessRightsTable createImmutableCopy() {
      AccessRightsTable rightsTable =
                           UserManagement.getInstance().getRightsTable();
      AccessRightsTable copyTable = new AccessRightsTable();
      User[] allUsers = rightsTable.getAllUsers();
      for (int i = 0; i < allUsers.length; i++) {
         User user = allUsers[i];
         Right[] rights = rightsTable.getUserRights(user);
         for (int j = 0; j < rights.length; j++) {
            Right right = rights[j];
            copyTable.grantRight(user, right);
         }
   }
      return copyTable;
      }
}


Listing Four
public class UserManagement {
   private static UserManagement ourSingleInstance;
   private AbstractAccessRightsTable myRightsTable;
   private UserManagement() {
      myRightsTable = new MutableAccessRightsTable(new AccessRightsTable());
   }
   public static UserManagement getInstance() {
      if(ourSingleInstance == null) {
         ourSingleInstance = new UserManagement();
      }
      return ourSingleInstance;
   }
   public AbstractAccessRightsTable getRightsTable() {
      return myRightsTable;
   }
}


Listing Five
public class MutableAccessRightsTable implements AbstractAccessRightsTable {
   private AbstractAccessRightsTable myDelegate;
   public MutableAccessRightsTable(AbstractAccessRightsTable pDelegate) {
      myDelegate = pDelegate;
   }
   public void grantRight(User user, Right right) {
      synchronized (this) {
         myDelegate.grantRight(user, right);
      }
   }
   public void revokeRight(User user, Right right) {
      synchronized (this) {
          myDelegate.revokeRight(user, right);
      }
   }
}


Listing Six
public class Client {
   public static void revokeCreateNewAccount() {...}
   public static AbstractAccessRightsTable createImmutableCopy() {
      AbstractAccessRightsTable rightsTable =
                             UserManagement.getInstance().getRightsTable();
      AbstractAccessRightsTable copyTable = new AccessRightsTable();
      User[] allUsers = rightsTable.getAllUsers();
      for (int i = 0; i < allUsers.length; i++) {
         User user = allUsers[i];
         Right[] rights = rightsTable.getUserRights(user);
         for (int j = 0; j < rights.length; j++) {
            Right right = rights[j];
            copyTable.grantRight(user, right);
         }
     }
     return new ImmutableAccessRightsTable(copyTable);
  }
}


Listing Seven
public class SampleClass {
   public Vector createSampleVector() {
      Vector vector = new Vector();
      vector.add("ItemOne");
      vector.add("ItemTwo");
      vector.add("ItemThree");
      return vector;
   }
   public void useSampleVector(Vector vector) {
      for (int i = 0; i < vector.size(); i++) {
         String s = (String) vector.elementAt(i);
         System.out.println("s = " + s);
      }
   }
}


Listing Eight
public class Vector extends ArrayList {
   public Object elementAt (int index) {
      return get(index);
   }
}


Listing Nine
public class SampleClass {
   public Vector createSampleVector() {
      Vector vector = new Vector();
      vector.add("ItemOne");
      vector.add("ItemTwo");
      vector.add("ItemThree");
      return vector;
   }
   public void useSampleVector(Vector vector) {
      for (int i = 0; i < vector.size(); i++) {
         String s = (String) vector.get(i);
         System.out.println("s = " + s);
      }
   }
}


Listing Ten
public class SampleClass {
   public ArrayList createSampleVector() {
      ArrayList vector = new ArrayList();
      vector.add("ItemOne");
      vector.add("ItemTwo");
      vector.add("ItemThree");
      return vector;
   }
   public void useSampleVector(ArrayList vector) {
      for (int i = 0; i < vector.size(); i++) {
         String s = (String) vector.get(i);
         System.out.println("s = " + s);
      }
   }
}


Listing Eleven
public class Sample {
   static final int READ_ACCESS = 1;
   static final int WRITE = 2;
   ArrayList myUsers;
   ArrayList myUsersWithRights;
   public void arrangeUsersByRight() {
      ... <some code here> ...
      ArrayList rightfulUsers = new ArrayList();
      for (int i = 0; i < myUsers.size(); i++) {
         User user = (User) myUsers.get(i);
         if ((user.myAccessRight & READ_ACCESS) != 0) {
            rightfulUsers.add(user);
         }
     }
     myUsersWithRights = rightfulUsers;
     ... <some code here> ...
   }
}


Listing Twelve
public void arrangeUsersByRight() {
      ... <some code here> ...
      ArrayList rightfulUsers = getUsersWithRight();
      myUsersWithRights = rightfulUsers;
      ... <some code here> ...
}
private ArrayList getUsersWithRight() {
      ArrayList rightfulUsers = new ArrayList();
      for (int i=0; i < myUsers.size(); i++) {
            User user = (User) myUsers.get(i);
            if ((user.myAccessRight & READ_ACCESS) != 0) {
                  rightfulUsers.add(user);
            }
      }
      return rightfulUsers;
}


Listing Thirteen
public void arrangeUsersByRight() {
      ...<some code here>...
      ArrayList rightfulUsers = getUsersWithRight(READ_ACCESS);
      myUsersWithRights = rightfulUsers;
      ...<some code here>...
}
private ArrayList getUsersWithRight(int pRight) {
      ArrayList rightfulUsers = new ArrayList();
      for (int i = 0; i < myUsers.size(); i++) {
            User user = (User) myUsers.get(i);
            if ((user.myAccessRight & pRight) !=0) {
                  rightfulUsers.add(user);
            }
      }
      return rightfulUsers;
}




