turt2live

Untitled

Mar 5th, 2013
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.46 KB | None | 0 0
  1. package com.turt2live.metrics.graph;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7.  
  8. import com.turt2live.metrics.Metrics.Graph;
  9. import com.turt2live.metrics.tracker.BasicTracker;
  10. import com.turt2live.metrics.tracker.LinkedTracker;
  11. import com.turt2live.metrics.tracker.Tracker;
  12.  
  13. /**
  14.  * Represents a donut graph
  15.  *
  16.  * @author turt2live
  17.  *
  18.  * @param <T> the type of donut graph
  19.  */
  20. public class DonutGraph<T extends Enum<T>> extends Graph {
  21.     /**
  22.      * Represents a donut slice
  23.      *
  24.      * @author Turt2live
  25.      *
  26.      * @param <T> the slice type
  27.      */
  28.     public static class DonutSlice<T extends Enum<T>> {
  29.         private LinkedTracker tracker;
  30.         private T object;
  31.         private String majorName, minorName;
  32.  
  33.         DonutSlice(T object, String majorName, String minorName){
  34.             this.tracker = new LinkedTracker(majorName + "~=~" + minorName, new BasicTracker("", majorName + "~=~" + minorName));
  35.             this.object = object;
  36.             this.majorName = majorName;
  37.             this.minorName = minorName;
  38.         }
  39.  
  40.         /**
  41.          * Gets the major name for this slice
  42.          *
  43.          * @return the major name
  44.          */
  45.         public String getMajorName(){
  46.             return majorName;
  47.         }
  48.  
  49.         /**
  50.          * Gets the minor name for this slice
  51.          *
  52.          * @return the minor name
  53.          */
  54.         public String getMinorName(){
  55.             return minorName;
  56.         }
  57.  
  58.         /**
  59.          * Converts the tracker for this slice
  60.          *
  61.          * @param tracker the new tracker
  62.          */
  63.         public void convertTracker(Tracker tracker){
  64.             if(tracker == null){
  65.                 throw new IllegalArgumentException("Cannot convert null trackers");
  66.             }
  67.             tracker.setValue(this.tracker.getValue());
  68.             this.tracker.setLink(tracker);
  69.         }
  70.  
  71.         /**
  72.          * Replaces the tracker for this slice
  73.          *
  74.          * @param tracker the new tracker
  75.          */
  76.         public void replaceTracker(Tracker tracker){
  77.             if(tracker == null){
  78.                 throw new IllegalArgumentException("Cannot replace null trackers");
  79.             }
  80.             this.tracker.setLink(tracker);
  81.         }
  82.  
  83.         /**
  84.          * Gets the original object for this slice
  85.          *
  86.          * @return the slice index
  87.          */
  88.         public T getObject(){
  89.             return object;
  90.         }
  91.     }
  92.  
  93.     private Map<T, List<DonutSlice<T>>> slices = new HashMap<T, List<DonutSlice<T>>>();
  94.  
  95.     /**
  96.      * Creates a new donut graph
  97.      *
  98.      * @param name the graph name
  99.      */
  100.     public DonutGraph(String name){
  101.         super(name);
  102.     }
  103.  
  104.     /**
  105.      * Adds a slice to the graph
  106.      *
  107.      * @param object the index
  108.      * @param majorName the major name
  109.      * @param minorName the minor name
  110.      * @return the created donut slice
  111.      */
  112.     public DonutSlice<T> addSlice(T object, String majorName, String minorName){
  113.         if(object == null || majorName == null || minorName == null){
  114.             throw new IllegalArgumentException("Null arguments are not allowed");
  115.         }
  116.         DonutSlice<T> slice = new DonutSlice<T>(object, majorName, minorName);
  117.         List<DonutSlice<T>> list = new ArrayList<DonutSlice<T>>();
  118.         list.add(slice);
  119.         if(slices.containsKey(object)){
  120.             list.addAll(slices.get(object));
  121.         }
  122.         slices.put(object, list);
  123.         addPlotter(slice.tracker);
  124.         return slice;
  125.     }
  126.  
  127.     /**
  128.      * Removes a slice from the graph
  129.      *
  130.      * @param object the slice index
  131.      */
  132.     public void removeSlice(T object){
  133.         if(object == null){
  134.             throw new IllegalArgumentException("Null arguments are not allowed");
  135.         }
  136.         List<DonutSlice<T>> last = slices.remove(object);
  137.         if(last != null){
  138.             for(DonutSlice<T> slice : last){
  139.                 removePlotter(slice.tracker);
  140.             }
  141.         }
  142.     }
  143.  
  144.     /**
  145.      * Removes a slice from the graph
  146.      *
  147.      * @param majorName the major name
  148.      * @param minorName the minor name
  149.      */
  150.     public void removeSlice(String majorName, String minorName){
  151.         if(majorName == null || minorName == null){
  152.             throw new IllegalArgumentException("Null arguments are not allowed");
  153.         }
  154.         for(T object : slices.keySet()){
  155.             List<DonutSlice<T>> list = slices.get(object);
  156.             if(list != null){
  157.                 for(DonutSlice<T> slice : list){
  158.                     if(slice.majorName.equalsIgnoreCase(majorName)
  159.                             && slice.minorName.equalsIgnoreCase(minorName)){
  160.                         removePlotter(slice.tracker);
  161.                     }
  162.                 }
  163.             }
  164.         }
  165.     }
  166.  
  167.     /**
  168.      * Gets a slice from the graph
  169.      *
  170.      * @param majorName the major name
  171.      * @param minorName the minor name
  172.      * @return the slice, or null if not found
  173.      */
  174.     public DonutSlice<T> getSlice(String majorName, String minorName){
  175.         if(majorName == null || minorName == null){
  176.             throw new IllegalArgumentException("Null arguments are not allowed");
  177.         }
  178.         for(T object : slices.keySet()){
  179.             List<DonutSlice<T>> list = slices.get(object);
  180.             if(list != null){
  181.                 for(DonutSlice<T> slice : list){
  182.                     if(slice == null){
  183.                         throw new IllegalArgumentException("Null slice found. Did you add the type?");
  184.                     }
  185.                     if(slice.majorName.equalsIgnoreCase(majorName)
  186.                             && slice.minorName.equalsIgnoreCase(minorName)){
  187.                         return slice;
  188.                     }
  189.                 }
  190.             }
  191.         }
  192.         return null;
  193.     }
  194.  
  195.     /**
  196.      * Gets a slice from the graph
  197.      *
  198.      * @param object the index
  199.      * @param minorName the minor name
  200.      * @return the slice, or null if not found
  201.      */
  202.     public DonutSlice<T> getSlice(T object, String minorName){
  203.         if(object == null || minorName == null){
  204.             throw new IllegalArgumentException("Null arguments are not allowed");
  205.         }
  206.         List<DonutSlice<T>> list = slices.get(object);
  207.         if(list != null){
  208.             for(DonutSlice<T> slice : list){
  209.                 if(slice == null){
  210.                     throw new IllegalArgumentException("Null slice found. Did you add the type?");
  211.                 }
  212.                 if(slice.minorName.equalsIgnoreCase(minorName)){
  213.                     return slice;
  214.                 }
  215.             }
  216.         }
  217.         return null;
  218.     }
  219.  
  220.     /**
  221.      * Increments a slice
  222.      *
  223.      * @param majorName the major name
  224.      * @param minorName the minor name
  225.      */
  226.     public void increment(String majorName, String minorName){
  227.         if(majorName == null || minorName == null){
  228.             throw new IllegalArgumentException("Null arguments are not allowed");
  229.         }
  230.         DonutSlice<T> slice = getSlice(majorName, minorName);
  231.         if(slice == null){
  232.             throw new IllegalArgumentException("Null slice found. Did you add the type?");
  233.         }
  234.         slice.tracker.increment();
  235.     }
  236.  
  237.     /**
  238.      * Increments a slice
  239.      *
  240.      * @param object the index
  241.      * @param minorName the minor name
  242.      */
  243.     public void increment(T object, String minorName){
  244.         if(object == null || minorName == null){
  245.             throw new IllegalArgumentException("Null arguments are not allowed");
  246.         }
  247.         DonutSlice<T> slice = getSlice(object, minorName);
  248.         if(slice == null){
  249.             throw new IllegalArgumentException("Null slice found. Did you add the type?");
  250.         }
  251.         slice.tracker.increment();
  252.     }
  253.  
  254. }
Advertisement
Add Comment
Please, Sign In to add comment