001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.ivy.core.module.status;
019:
020: import java.util.ArrayList;
021: import java.util.Arrays;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.ListIterator;
026: import java.util.Map;
027:
028: import org.apache.ivy.core.IvyContext;
029: import org.apache.ivy.util.Message;
030:
031: /**
032: * Note: update methods (such as addStatus) should only be called BEFORE any call to accessor
033: * methods
034: */
035: public class StatusManager {
036: public static StatusManager newDefaultInstance() {
037: return new StatusManager(new Status[] {
038: new Status("release", false),
039: new Status("milestone", false),
040: new Status("integration", true) }, "integration");
041: }
042:
043: public static StatusManager getCurrent() {
044: return IvyContext.getContext().getSettings().getStatusManager();
045: }
046:
047: private List status = new ArrayList();
048:
049: private String defaultStatus;
050:
051: // for easier querying only
052: private Map statusPriorityMap;
053:
054: private Map statusIntegrationMap;
055:
056: private String deliveryStatusListString;
057:
058: public StatusManager(Status[] status, String defaultStatus) {
059: this .status.addAll(Arrays.asList(status));
060: this .defaultStatus = defaultStatus;
061:
062: computeMaps();
063: }
064:
065: public StatusManager() {
066: }
067:
068: public void addStatus(Status status) {
069: this .status.add(status);
070: }
071:
072: public void setDefaultStatus(String defaultStatus) {
073: this .defaultStatus = defaultStatus;
074: }
075:
076: public List getStatuses() {
077: return status;
078: }
079:
080: private void computeMaps() {
081: if (status.isEmpty()) {
082: throw new IllegalStateException(
083: "badly configured statuses: no status found");
084: }
085: statusPriorityMap = new HashMap();
086: for (ListIterator iter = status.listIterator(); iter.hasNext();) {
087: Status status = (Status) iter.next();
088: statusPriorityMap.put(status.getName(), new Integer(iter
089: .previousIndex()));
090: }
091: statusIntegrationMap = new HashMap();
092: for (Iterator iter = status.iterator(); iter.hasNext();) {
093: Status status = (Status) iter.next();
094: statusIntegrationMap.put(status.getName(), Boolean
095: .valueOf(status.isIntegration()));
096: }
097: }
098:
099: public boolean isStatus(String status) {
100: if (statusPriorityMap == null) {
101: computeMaps();
102: }
103: return statusPriorityMap.containsKey(status);
104: }
105:
106: public int getPriority(String status) {
107: if (statusPriorityMap == null) {
108: computeMaps();
109: }
110: Integer priority = (Integer) statusPriorityMap.get(status);
111: if (priority == null) {
112: Message.debug("unknown status " + status
113: + ": assuming lowest priority");
114: return Integer.MAX_VALUE;
115: }
116: return priority.intValue();
117: }
118:
119: public boolean isIntegration(String status) {
120: if (statusIntegrationMap == null) {
121: computeMaps();
122: }
123: Boolean isIntegration = (Boolean) statusIntegrationMap
124: .get(status);
125: if (isIntegration == null) {
126: Message.debug("unknown status " + status
127: + ": assuming integration");
128: return true;
129: }
130: return isIntegration.booleanValue();
131: }
132:
133: public String getDeliveryStatusListString() {
134: if (deliveryStatusListString == null) {
135: StringBuffer ret = new StringBuffer();
136: for (Iterator iter = status.iterator(); iter.hasNext();) {
137: Status status = (Status) iter.next();
138: if (!status.isIntegration()) {
139: ret.append(status.getName()).append(",");
140: }
141: }
142: if (ret.length() > 0) {
143: ret.deleteCharAt(ret.length() - 1);
144: }
145: deliveryStatusListString = ret.toString();
146: }
147: return deliveryStatusListString;
148: }
149:
150: public String getDefaultStatus() {
151: if (defaultStatus == null) {
152: if (status.isEmpty()) {
153: throw new IllegalStateException(
154: "badly configured statuses: no status found");
155: }
156: defaultStatus = ((Status) status.get(status.size() - 1))
157: .getName();
158: }
159: return defaultStatus;
160: }
161: }
|