Skip to main content
summaryrefslogtreecommitdiffstats
blob: 471f401cf2b8a1e0ed823e0f56d1d781da502f2e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*******************************************************************************
 * Copyright (c) 2009 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.core.util;

import java.util.Arrays;
import java.util.Collection;
import org.eclipse.osee.framework.core.exception.OseeArgumentException;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.GUID;

/**
 * @author Roberto E. Escobar
 */
public final class Conditions {

   private Conditions() {
      // Utility Class
   }

   public static void checkNotNull(Object object, String objectName) throws OseeCoreException {
      if (object == null) {
         throw new OseeArgumentException(String.format("%s cannot be null", objectName));
      }
   }

   public static void checkNotNull(Object object, String objectName, String details, Object... data) throws OseeCoreException {
      if (object == null) {
         String message = String.format(details, data);
         throw new OseeArgumentException(String.format("%s cannot be null - %s", objectName, message));
      }
   }

   public static void checkNotNullOrEmpty(String object, String objectName) throws OseeCoreException {
      checkNotNull(object, objectName);
      if (object.length() == 0) {
         throw new OseeArgumentException(String.format("%s cannot be empty", objectName));
      }
   }

   public static void checkNotNullOrEmpty(Collection<? extends Object> collection, String objectName) throws OseeCoreException {
      checkNotNull(collection, objectName);
      if (collection.isEmpty()) {
         throw new OseeArgumentException("%s cannot be empty", objectName);
      }
   }

   public static void checkNotNullOrEmpty(String object, String objectName, String details, Object... data) throws OseeCoreException {
      checkNotNull(object, objectName);
      if (object.length() == 0) {
         String message = String.format(details, data);
         throw new OseeArgumentException(String.format("%s cannot be empty - %s", objectName, message));
      }
   }

   public static void checkExpressionFailOnTrue(boolean result, String message, Object... data) throws OseeCoreException {
      if (result) {
         throw new OseeArgumentException(String.format(message, data));
      }
   }

   public static String checkGuidCreateIfNeeded(String guid) {
      String toReturn = guid;
      if (guid == null) {
         toReturn = GUID.create();
      }
      return toReturn;
   }

   public static void checkDoesNotContainNulls(Object object, String message, Object... data) throws OseeCoreException {
      checkNotNull(object, message);
      Collection<?> toCheck = null;
      if (object instanceof Collection<?>) {
         toCheck = (Collection<?>) object;
      } else if (object instanceof Object[]) {
         toCheck = Arrays.asList((Object[]) object);
      }
      if (toCheck != null) {
         for (Object item : toCheck) {
            if (item == null) {
               throw new OseeArgumentException(String.format(message, data));
            }
         }
      } else {
         throw new OseeArgumentException("object is not an array or a collection");
      }
   }
}

Back to the top