]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 IBM Corporation and others. | |
3 | * All rights reserved. This program and the accompanying materials | |
4 | * are made available under the terms of the Eclipse Public License v1.0 | |
5 | * which accompanies this distribution, and is available at | |
6 | * http://www.eclipse.org/legal/epl-v10.html | |
7 | * | |
8 | * Contributors: | |
9 | * IBM Corporation - initial API and implementation | |
10 | *******************************************************************************/ | |
11 | package org.eclipse.jdt.internal.corext.refactoring.typeconstraints; | |
12 | ||
13 | import java.util.Arrays; | |
14 | import java.util.Comparator; | |
15 | import java.util.List; | |
16 | ||
17 | import org.eclipse.core.runtime.Assert; | |
18 | ||
19 | ||
20 | public class CompositeOrTypeConstraint implements ITypeConstraint{ | |
21 | ||
22 | private final ITypeConstraint[] fConstraints; | |
23 | ||
24 | /* package */ CompositeOrTypeConstraint(ITypeConstraint[] constraints){ | |
25 | Assert.isNotNull(constraints); | |
26 | fConstraints= sort(getCopy(constraints)); | |
27 | } | |
28 | ||
29 | private static ITypeConstraint[] getCopy(ITypeConstraint[] constraints) { | |
30 | List<ITypeConstraint> l= Arrays.asList(constraints); | |
31 | return l.toArray(new ITypeConstraint[l.size()]); | |
32 | } | |
33 | ||
34 | private static ITypeConstraint[] sort(ITypeConstraint[] constraints) { | |
35 | //TODO bogus to sort by toString - will have to come up with something better | |
36 | Arrays.sort(constraints, new Comparator<ITypeConstraint>(){ | |
37 | public int compare(ITypeConstraint o1, ITypeConstraint o2) { | |
38 | return o2.toString().compareTo(o1.toString()); | |
39 | } | |
40 | }); | |
41 | return constraints; | |
42 | } | |
43 | ||
44 | /* (non-Javadoc) | |
45 | * @see org.eclipse.jdt.internal.corext.refactoring.experiments.ITypeConstraint#toResolvedString() | |
46 | */ | |
47 | public String toResolvedString() { | |
48 | StringBuffer buff= new StringBuffer(); | |
49 | for (int i= 0; i < fConstraints.length; i++) { | |
50 | ITypeConstraint constraint= fConstraints[i]; | |
51 | if (i > 0) | |
52 | buff.append(" or "); //$NON-NLS-1$ | |
53 | buff.append(constraint.toResolvedString()); | |
54 | } | |
55 | return buff.toString(); | |
56 | } | |
57 | ||
58 | /* (non-Javadoc) | |
59 | * @see org.eclipse.jdt.internal.corext.refactoring.experiments.ITypeConstraint#isSimpleTypeConstraint() | |
60 | */ | |
61 | public boolean isSimpleTypeConstraint() { | |
62 | return false; | |
63 | } | |
64 | ||
65 | /* (non-Javadoc) | |
66 | * @see java.lang.Object#toString() | |
67 | */ | |
68 | @Override | |
69 | public String toString() { | |
70 | StringBuffer buff= new StringBuffer(); | |
71 | for (int i= 0; i < fConstraints.length; i++) { | |
72 | ITypeConstraint constraint= fConstraints[i]; | |
73 | if (i > 0) | |
74 | buff.append(" or "); //$NON-NLS-1$ | |
75 | buff.append(constraint.toString()); | |
76 | } | |
77 | return buff.toString(); | |
78 | } | |
79 | ||
80 | public ITypeConstraint[] getConstraints() { | |
81 | return fConstraints; | |
82 | } | |
83 | } |