Richard S. Hall | 85bafab | 2009-07-13 13:25:46 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
| 20 | package org.cauldron.bld.core.internal.model.osgi; |
| 21 | |
| 22 | import org.cauldron.sigil.model.AbstractModelElement; |
| 23 | import org.cauldron.sigil.model.IModelElement; |
| 24 | import org.cauldron.sigil.model.InvalidModelException; |
| 25 | import org.cauldron.sigil.model.common.VersionRange; |
| 26 | import org.cauldron.sigil.model.osgi.IPackageExport; |
| 27 | import org.cauldron.sigil.model.osgi.IPackageImport; |
| 28 | |
| 29 | public class PackageImport extends AbstractModelElement implements IPackageImport { |
| 30 | |
| 31 | private static final long serialVersionUID = 1L; |
| 32 | |
| 33 | private String name; |
| 34 | private VersionRange versions = VersionRange.ANY_VERSION; |
| 35 | |
| 36 | // resolution directive |
| 37 | private boolean optional; |
| 38 | private boolean dependency = true; |
| 39 | private OSGiImport osgiImport = OSGiImport.AUTO; |
| 40 | |
| 41 | public PackageImport() { |
| 42 | super( "OSGi Package Import" ); |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public void checkValid() throws InvalidModelException { |
| 47 | if ( name == null ) { |
| 48 | throw new InvalidModelException( this, "Package name must be set" ); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /* (non-Javadoc) |
| 53 | * @see org.cauldron.sigil.internal.model.osgi.IPackageImport#isOptional() |
| 54 | */ |
| 55 | public boolean isOptional() { |
| 56 | return optional; |
| 57 | } |
| 58 | |
| 59 | /* (non-Javadoc) |
| 60 | * @see org.cauldron.sigil.internal.model.osgi.IPackageImport#setOptional(boolean) |
| 61 | */ |
| 62 | public void setOptional(boolean optional) { |
| 63 | this.optional = optional; |
| 64 | } |
| 65 | |
| 66 | public boolean isDependency() { |
| 67 | return dependency; |
| 68 | } |
| 69 | |
| 70 | public void setDependency(boolean dependency) { |
| 71 | this.dependency = dependency; |
| 72 | } |
| 73 | |
| 74 | public OSGiImport getOSGiImport() { |
| 75 | return osgiImport; |
| 76 | } |
| 77 | |
| 78 | public void setOSGiImport(OSGiImport osgiHeader) { |
| 79 | this.osgiImport = osgiHeader; |
| 80 | } |
| 81 | |
| 82 | public String getPackageName() { |
| 83 | return name; |
| 84 | } |
| 85 | |
| 86 | /* (non-Javadoc) |
| 87 | * @see org.cauldron.sigil.internal.model.osgi.IPackageImport#setName(java.lang.String) |
| 88 | */ |
| 89 | public void setPackageName(String name) { |
| 90 | this.name = name; |
| 91 | } |
| 92 | |
| 93 | /* (non-Javadoc) |
| 94 | * @see org.cauldron.sigil.internal.model.osgi.IPackageImport#getVersion() |
| 95 | */ |
| 96 | public VersionRange getVersions() { |
| 97 | return versions; |
| 98 | } |
| 99 | |
| 100 | /* (non-Javadoc) |
| 101 | * @see org.cauldron.sigil.internal.model.osgi.IPackageImport#setVersion(java.lang.String) |
| 102 | */ |
| 103 | public void setVersions(VersionRange versions) { |
| 104 | this.versions = versions == null ? VersionRange.ANY_VERSION : versions; |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public String toString() { |
| 109 | return "Package-Import[" + name + ":" + versions + ":" + (optional ? "optional" : "mandatory") + "]"; |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public boolean equals(Object obj) { |
| 114 | if (this == obj) |
| 115 | return true; |
| 116 | if (obj == null) |
| 117 | return false; |
| 118 | |
| 119 | if ( obj instanceof PackageImport ) { |
| 120 | PackageImport pi = (PackageImport) obj; |
| 121 | return name.equals( pi.name ) && versions.equals( pi.versions ) && optional == pi.optional; |
| 122 | } |
| 123 | else { |
| 124 | return false; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public int hashCode() { |
| 130 | int hc = name.hashCode() * versions.hashCode(); |
| 131 | |
| 132 | if ( optional ) { |
| 133 | hc *= -1; |
| 134 | } |
| 135 | |
| 136 | return hc; |
| 137 | } |
| 138 | |
| 139 | public boolean accepts(IModelElement provider) { |
| 140 | if ( provider instanceof IPackageExport ) { |
| 141 | IPackageExport pe = (IPackageExport) provider; |
| 142 | return pe.getPackageName().equals( name ) && versions.contains( pe.getVersion() ); |
| 143 | } |
| 144 | else { |
| 145 | return false; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | public int compareTo(IPackageImport o) { |
| 150 | int i = name.compareTo(o.getPackageName()); |
| 151 | |
| 152 | if ( i == 0 ) { |
| 153 | i = compareVersion(o.getVersions()); |
| 154 | } |
| 155 | |
| 156 | return i; |
| 157 | } |
| 158 | |
| 159 | private int compareVersion(VersionRange range) { |
| 160 | if ( versions == null ) { |
| 161 | if ( range == null ) { |
| 162 | return 0; |
| 163 | } |
| 164 | else { |
| 165 | return 1; |
| 166 | } |
| 167 | } |
| 168 | else { |
| 169 | if ( range == null ) { |
| 170 | return -1; |
| 171 | } |
| 172 | else { |
| 173 | return versions.getCeiling().compareTo(range.getCeiling()); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | } |