View Javadoc

1   package org.riverock.dbrevision.manager.patch;
2   
3   import java.util.List;
4   import java.util.ArrayList;
5   import java.util.Iterator;
6   
7   import org.riverock.dbrevision.annotation.schema.db.Patch;
8   import org.riverock.dbrevision.exception.TwoPatchesWithEmptyPreviousPatchException;
9   import org.riverock.dbrevision.exception.FirstPatchNotFoundException;
10  import org.riverock.dbrevision.exception.TwoPatchesWithSameNameException;
11  import org.riverock.dbrevision.exception.NoChildPatchFoundException;
12  
13  /**
14   * User: SMaslyukov
15   * Date: 02.08.2007
16   * Time: 15:26:14
17   */
18  public class PatchSorter {
19      
20      public static List<Patch> sort(List<Patch> patches) {
21          // looking for 1st element
22          Patch firstPatch=null;
23          List<Patch> list = new ArrayList<Patch>(patches.size());
24          for (Patch patch : patches) {
25              patch.setProcessed(false);
26              if (patch.getPreviousName()==null) {
27                  if (firstPatch!=null) {
28                      throw new TwoPatchesWithEmptyPreviousPatchException("Patch name #1: " +firstPatch.getName()+", patch name #2: " +patch.getName());
29                  }
30                  firstPatch = patch;
31              }
32              else {
33                  list.add(patch);
34              }
35          }
36          if (firstPatch==null) {
37              throw new FirstPatchNotFoundException();
38          }
39  
40          checkDuplicateNames(patches);
41  
42          List<Patch> result = new ArrayList<Patch>(patches.size());
43          Patch current = firstPatch;
44          while (!list.isEmpty()) {
45              result.add(current);
46              Iterator<Patch> it = list.iterator();
47              boolean isFound=false;
48              while (it.hasNext()) {
49                  Patch patch = it.next();
50                  if (current.getName().equals(patch.getPreviousName())) {
51                      current = patch;
52                      it.remove();
53                      isFound=true;
54                      break;
55                  }
56              }
57              if (!isFound && !list.isEmpty()) {
58                  throw new NoChildPatchFoundException("patch name: " +current.getName());
59              }
60          }
61          result.add(current);
62          return result;
63      }
64  
65      private static void checkDuplicateNames(List<Patch> patches) {
66          List<Patch> result = new ArrayList<Patch>(patches);
67          while (!result.isEmpty()) {
68              if (patches.size()==1) {
69                  break;
70              }
71              Patch current = result.get(0);
72              result.remove(0);
73              for (Patch patch : result) {
74                  if (current.getName().equals(patch.getName())) {
75                      throw new TwoPatchesWithSameNameException("Patch name: " + current.getName());
76                  }
77              }
78          }
79      }
80  }