001 package org.hackystat.telemetry.analyzer.configuration; 002 003 import org.hackystat.sensorbase.resource.projects.jaxb.Project; 004 005 /** 006 * The level of sharing associated with a telemetry chart or report definition. 007 * The share scope can be global, project or private (not shared). 008 * <p> 009 * V8 Notes: Initially, all scope will be global. This is an enumerated type and 010 * really should be implemented that way. 011 * 012 * @author (Cedric) Qin Zhang 013 */ 014 public class ShareScope { 015 016 private static final int GLOBAL = 100; 017 private static final int PROJECT = 200; 018 private static final int PRIVATE = 300; 019 020 private static final String GLOBAL_STRING = "Global"; 021 private static final String PRIVATE_STRING = "Not Shared"; 022 //private static final String PROJECT_PREFIX_STRING = "Project@@@"; 023 024 private static final ShareScope GLOBAL_SHARE_SCOPE = new ShareScope(GLOBAL, null); 025 private static final ShareScope PRIVATE_SHARE_SCOPE = new ShareScope(PRIVATE, null); 026 027 private int scope; 028 private Project project; 029 030 /** 031 * Private constructor. User cannot call this method. 032 * 033 * @param shareScope The share scope. 034 * @param project The project. It must be null if share scope is global or 035 * private. It must not be null if share scope is project. 036 */ 037 private ShareScope(int shareScope, Project project) { 038 this.scope = shareScope; 039 this.project = project; 040 } 041 042 /** 043 * Gets the global share scope. 044 * 045 * @return The global share scope. 046 */ 047 public static ShareScope getGlobalShareScope() { 048 return GLOBAL_SHARE_SCOPE; 049 } 050 051 /** 052 * Gets the project share scope. 053 * 054 * @param project The project. 055 * @return The project share scope. Always Global in V8. 056 */ 057 public static ShareScope getProjectShareScope(Project project) { 058 if (project == null) { 059 throw new IllegalArgumentException("Project cannot be null"); 060 } 061 return new ShareScope(PROJECT, project); 062 } 063 064 /** 065 * Gets the private share scope. 066 * 067 * @return The private share scope. 068 */ 069 public static ShareScope getPrivateShareScope() { 070 return PRIVATE_SHARE_SCOPE; 071 } 072 073 /** 074 * Gets the project if the the share scope is project. 075 * 076 * @return The project. 077 * 078 * @throws TelemetryConfigurationException If the share scope is not project. 079 */ 080 public Project getProject() throws TelemetryConfigurationException { 081 if (!this.isProject()) { 082 throw new TelemetryConfigurationException("The share scope is not project."); 083 } 084 return this.project; 085 } 086 087 /** 088 * Checks whether is share scope is global. 089 * 090 * @return True if share scope is global. 091 */ 092 public boolean isGlobal() { 093 return GLOBAL == this.scope; 094 } 095 096 /** 097 * Checks whether is share scope is project. 098 * 099 * @return True if share scope is project. 100 */ 101 public boolean isProject() { 102 return PROJECT == this.scope; 103 } 104 105 /** 106 * Checks whether is share scope is private. 107 * 108 * @return True if share scope is private. 109 */ 110 public boolean isPrivate() { 111 return PRIVATE == this.scope; 112 } 113 114 /** 115 * Gets the string representation of this instance. 116 * 117 * @return The string representation. 118 */ 119 @Override 120 public String toString() { 121 if (this.isGlobal()) { 122 return GLOBAL_STRING; 123 } 124 else if (this.isProject()) { 125 return "Project: " + this.project.getName(); 126 } 127 else { 128 // must be private 129 return PRIVATE_STRING; 130 } 131 } 132 133 /** 134 * Serialized this instance to a string. Note that this string may not be user 135 * readable. 136 * 137 * @return The serialized form. 138 */ 139 public String serializeToString() { 140 if (this.isGlobal()) { 141 return GLOBAL_STRING; 142 } 143 else if (this.isProject()) { 144 return "Project@@@" + this.project.getName(); 145 } 146 else { 147 // must be private 148 return PRIVATE_STRING; 149 } 150 } 151 152 /** 153 * Tests whether this instance equals another instance. 154 * 155 * @param obj Another instance. 156 * 157 * @return True if they are equal. 158 */ 159 @Override 160 public boolean equals(Object obj) { 161 if (!(obj instanceof ShareScope)) { 162 return false; 163 } 164 ShareScope another = (ShareScope) obj; 165 if (this.scope == another.scope) { 166 if (this.project == null) { 167 return another.project == null; 168 } 169 else { 170 return this.project.equals(another.project); 171 } 172 } 173 else { 174 return false; 175 } 176 } 177 178 /** 179 * Gets the hash code. 180 * 181 * @return The hash code. 182 */ 183 @Override 184 public int hashCode() { 185 return (this.project == null) ? this.scope : this.project.hashCode(); 186 } 187 }