Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.turt2live.metrics.graph;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import com.turt2live.metrics.Metrics.Graph;
- import com.turt2live.metrics.tracker.BasicTracker;
- import com.turt2live.metrics.tracker.LinkedTracker;
- import com.turt2live.metrics.tracker.Tracker;
- /**
- * Represents a donut graph
- *
- * @author turt2live
- *
- * @param <T> the type of donut graph
- */
- public class DonutGraph<T extends Enum<T>> extends Graph {
- /**
- * Represents a donut slice
- *
- * @author Turt2live
- *
- * @param <T> the slice type
- */
- public static class DonutSlice<T extends Enum<T>> {
- private LinkedTracker tracker;
- private T object;
- private String majorName, minorName;
- DonutSlice(T object, String majorName, String minorName){
- this.tracker = new LinkedTracker(majorName + "~=~" + minorName, new BasicTracker("", majorName + "~=~" + minorName));
- this.object = object;
- this.majorName = majorName;
- this.minorName = minorName;
- }
- /**
- * Gets the major name for this slice
- *
- * @return the major name
- */
- public String getMajorName(){
- return majorName;
- }
- /**
- * Gets the minor name for this slice
- *
- * @return the minor name
- */
- public String getMinorName(){
- return minorName;
- }
- /**
- * Converts the tracker for this slice
- *
- * @param tracker the new tracker
- */
- public void convertTracker(Tracker tracker){
- if(tracker == null){
- throw new IllegalArgumentException("Cannot convert null trackers");
- }
- tracker.setValue(this.tracker.getValue());
- this.tracker.setLink(tracker);
- }
- /**
- * Replaces the tracker for this slice
- *
- * @param tracker the new tracker
- */
- public void replaceTracker(Tracker tracker){
- if(tracker == null){
- throw new IllegalArgumentException("Cannot replace null trackers");
- }
- this.tracker.setLink(tracker);
- }
- /**
- * Gets the original object for this slice
- *
- * @return the slice index
- */
- public T getObject(){
- return object;
- }
- }
- private Map<T, List<DonutSlice<T>>> slices = new HashMap<T, List<DonutSlice<T>>>();
- /**
- * Creates a new donut graph
- *
- * @param name the graph name
- */
- public DonutGraph(String name){
- super(name);
- }
- /**
- * Adds a slice to the graph
- *
- * @param object the index
- * @param majorName the major name
- * @param minorName the minor name
- * @return the created donut slice
- */
- public DonutSlice<T> addSlice(T object, String majorName, String minorName){
- if(object == null || majorName == null || minorName == null){
- throw new IllegalArgumentException("Null arguments are not allowed");
- }
- DonutSlice<T> slice = new DonutSlice<T>(object, majorName, minorName);
- List<DonutSlice<T>> list = new ArrayList<DonutSlice<T>>();
- list.add(slice);
- if(slices.containsKey(object)){
- list.addAll(slices.get(object));
- }
- slices.put(object, list);
- addPlotter(slice.tracker);
- return slice;
- }
- /**
- * Removes a slice from the graph
- *
- * @param object the slice index
- */
- public void removeSlice(T object){
- if(object == null){
- throw new IllegalArgumentException("Null arguments are not allowed");
- }
- List<DonutSlice<T>> last = slices.remove(object);
- if(last != null){
- for(DonutSlice<T> slice : last){
- removePlotter(slice.tracker);
- }
- }
- }
- /**
- * Removes a slice from the graph
- *
- * @param majorName the major name
- * @param minorName the minor name
- */
- public void removeSlice(String majorName, String minorName){
- if(majorName == null || minorName == null){
- throw new IllegalArgumentException("Null arguments are not allowed");
- }
- for(T object : slices.keySet()){
- List<DonutSlice<T>> list = slices.get(object);
- if(list != null){
- for(DonutSlice<T> slice : list){
- if(slice.majorName.equalsIgnoreCase(majorName)
- && slice.minorName.equalsIgnoreCase(minorName)){
- removePlotter(slice.tracker);
- }
- }
- }
- }
- }
- /**
- * Gets a slice from the graph
- *
- * @param majorName the major name
- * @param minorName the minor name
- * @return the slice, or null if not found
- */
- public DonutSlice<T> getSlice(String majorName, String minorName){
- if(majorName == null || minorName == null){
- throw new IllegalArgumentException("Null arguments are not allowed");
- }
- for(T object : slices.keySet()){
- List<DonutSlice<T>> list = slices.get(object);
- if(list != null){
- for(DonutSlice<T> slice : list){
- if(slice == null){
- throw new IllegalArgumentException("Null slice found. Did you add the type?");
- }
- if(slice.majorName.equalsIgnoreCase(majorName)
- && slice.minorName.equalsIgnoreCase(minorName)){
- return slice;
- }
- }
- }
- }
- return null;
- }
- /**
- * Gets a slice from the graph
- *
- * @param object the index
- * @param minorName the minor name
- * @return the slice, or null if not found
- */
- public DonutSlice<T> getSlice(T object, String minorName){
- if(object == null || minorName == null){
- throw new IllegalArgumentException("Null arguments are not allowed");
- }
- List<DonutSlice<T>> list = slices.get(object);
- if(list != null){
- for(DonutSlice<T> slice : list){
- if(slice == null){
- throw new IllegalArgumentException("Null slice found. Did you add the type?");
- }
- if(slice.minorName.equalsIgnoreCase(minorName)){
- return slice;
- }
- }
- }
- return null;
- }
- /**
- * Increments a slice
- *
- * @param majorName the major name
- * @param minorName the minor name
- */
- public void increment(String majorName, String minorName){
- if(majorName == null || minorName == null){
- throw new IllegalArgumentException("Null arguments are not allowed");
- }
- DonutSlice<T> slice = getSlice(majorName, minorName);
- if(slice == null){
- throw new IllegalArgumentException("Null slice found. Did you add the type?");
- }
- slice.tracker.increment();
- }
- /**
- * Increments a slice
- *
- * @param object the index
- * @param minorName the minor name
- */
- public void increment(T object, String minorName){
- if(object == null || minorName == null){
- throw new IllegalArgumentException("Null arguments are not allowed");
- }
- DonutSlice<T> slice = getSlice(object, minorName);
- if(slice == null){
- throw new IllegalArgumentException("Null slice found. Did you add the type?");
- }
- slice.tracker.increment();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment