Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CollectionUtil |
|
| 3.25;3.25 |
1 | /* $Id: CollectionUtil.java 17705 2009-12-23 16:43:29Z bobtarling $ | |
2 | ******************************************************************************* | |
3 | * Copyright (c) 2009 Contributors - see below | |
4 | * All rights reserved. This program and the accompanying materials | |
5 | * are made available under the terms of the Eclipse Public License v1.0 | |
6 | * which accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Bob Tarling | |
11 | ******************************************************************************* | |
12 | */ | |
13 | ||
14 | // $Id: CollectionUtil.java 17705 2009-12-23 16:43:29Z bobtarling $ | |
15 | // Copyright (c) 1996-2006 The Regents of the University of California. All | |
16 | // Rights Reserved. Permission to use, copy, modify, and distribute this | |
17 | // software and its documentation without fee, and without a written | |
18 | // agreement is hereby granted, provided that the above copyright notice | |
19 | // and this paragraph appear in all copies. This software program and | |
20 | // documentation are copyrighted by The Regents of the University of | |
21 | // California. The software program and documentation are supplied "AS | |
22 | // IS", without any accompanying services from The Regents. The Regents | |
23 | // does not warrant that the operation of the program will be | |
24 | // uninterrupted or error-free. The end-user understands that the program | |
25 | // was developed for research purposes and is advised not to rely | |
26 | // exclusively on the program for any reason. IN NO EVENT SHALL THE | |
27 | // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, | |
28 | // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, | |
29 | // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF | |
30 | // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF | |
31 | // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY | |
32 | // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
33 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE | |
34 | // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF | |
35 | // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, | |
36 | // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | |
37 | ||
38 | package org.argouml.util; | |
39 | ||
40 | import java.util.Collection; | |
41 | import java.util.List; | |
42 | ||
43 | /** | |
44 | * Some helper methods for dealing with collections. | |
45 | * | |
46 | * @author Bob Tarling | |
47 | * @stereotype utility | |
48 | */ | |
49 | public final class CollectionUtil { | |
50 | ||
51 | /** | |
52 | * Can't construct a utility. | |
53 | */ | |
54 | 0 | private CollectionUtil() { |
55 | 0 | } |
56 | ||
57 | /** | |
58 | * Return the first item from a collection using the most efficient | |
59 | * method possible. | |
60 | * | |
61 | * @param c The Collection. | |
62 | * @return the first element of a Collection. | |
63 | * @throws java.util.NoSuchElementException if the collection is empty. | |
64 | */ | |
65 | public static Object getFirstItem(Collection c) { | |
66 | 0 | if (c instanceof List) { |
67 | 0 | return ((List) c).get(0); |
68 | } | |
69 | 0 | return c.iterator().next(); |
70 | } | |
71 | ||
72 | /** | |
73 | * Return the first item from a collection using the most efficient | |
74 | * method possible. Returns null for an empty collection. | |
75 | * | |
76 | * @param c The Collection. | |
77 | * @return the first element of a Collection. | |
78 | */ | |
79 | public static Object getFirstItemOrNull(Collection c) { | |
80 | 0 | if (c.size() == 0) { |
81 | 0 | return null; |
82 | } | |
83 | 0 | return getFirstItem(c); |
84 | } | |
85 | ||
86 | /** | |
87 | * Get the index position of an element in a collection | |
88 | * | |
89 | * @param c The Collection. | |
90 | * @param elem the element to find the index of | |
91 | * @return the element index | |
92 | */ | |
93 | public static int indexOf( | |
94 | final Collection c, | |
95 | final Object elem) { | |
96 | 0 | if (c instanceof List) { |
97 | 0 | return ((List) c).indexOf(elem); |
98 | } else { | |
99 | 0 | int index = 0; |
100 | 0 | for (Object element : c) { |
101 | 0 | if (element == elem) { |
102 | 0 | return index; |
103 | } else { | |
104 | 0 | ++index; |
105 | } | |
106 | } | |
107 | 0 | return -1; |
108 | } | |
109 | } | |
110 | } |