Pkg.java

00001 /*
00002  * Copyright (c) 2003-2006, KNOPFLERFISH project
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following
00007  * conditions are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  *
00012  * - Redistributions in binary form must reproduce the above
00013  *   copyright notice, this list of conditions and the following
00014  *   disclaimer in the documentation and/or other materials
00015  *   provided with the distribution.
00016  *
00017  * - Neither the name of the KNOPFLERFISH project nor the names of its
00018  *   contributors may be used to endorse or promote products derived
00019  *   from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00027  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00028  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
00030  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00031  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
00032  * OF THE POSSIBILITY OF SUCH DAMAGE.
00033  */
00034 
00035 package org.knopflerfish.framework;
00036 
00037 import java.util.*;
00038 
00039 
00045 class Pkg {
00046 
00047   final String pkg;
00048 
00049   ArrayList /* ExportPkg */ exporters = new ArrayList(1);
00050 
00051   ArrayList /* ImportPkg */ importers = new ArrayList();
00052 
00053   ArrayList /* ExportPkg */ providers = new ArrayList(1);
00054 
00055 
00059   Pkg(String pkg) {
00060     this.pkg = pkg;
00061   }
00062 
00063 
00069   synchronized void addExporter(ExportPkg ep) {
00070     int i = Math.abs(Util.binarySearch(exporters, epComp, ep) + 1);
00071     exporters.add(i, ep);
00072     ep.attachPkg(this);
00073   }
00074 
00075 
00082   synchronized boolean removeExporter(ExportPkg p) {
00083     providers.remove(p);
00084     exporters.remove(p);
00085     p.detachPkg();
00086     return true;
00087   }
00088 
00089 
00095   synchronized void addImporter(ImportPkg ip) {
00096     int i = Math.abs(Util.binarySearch(importers, ipComp, ip) + 1);
00097     importers.add(i, ip);
00098     ip.attachPkg(this);
00099   }
00100 
00101 
00107   synchronized void removeImporter(ImportPkg ip) {
00108     importers.remove(ip);
00109     ip.detachPkg();
00110   }
00111 
00112 
00119   synchronized void addProvider(ExportPkg ep) {
00120     int i = Util.binarySearch(providers, epComp, ep);
00121     if (i < 0) {
00122       providers.add(-i - 1, ep);
00123     }
00124   }
00125 
00126 
00133   synchronized ExportPkg getBestProvider() {
00134     if (!providers.isEmpty()) {
00135       return (ExportPkg)providers.get(0);
00136     }
00137     return null;
00138   }
00139 
00140 
00146   synchronized boolean isEmpty() {
00147     return exporters.size() == 0 && importers.size() == 0;
00148   }
00149 
00150 
00151   public String toString() {
00152     return toString(2);
00153   }
00154 
00155 
00156   public String toString(int level) {
00157     StringBuffer sb = new StringBuffer();
00158     sb.append("Pkg[");
00159 
00160     if(level > 0) {
00161       sb.append("pkg=" + pkg);
00162     }
00163     if(level > 1) {
00164       sb.append(", providers=" + providers);
00165     }
00166     if(level > 2) {
00167       sb.append(", exporters=" + exporters);
00168     }
00169     sb.append("]");
00170 
00171     return sb.toString();
00172   }
00173 
00174 
00175   static final Util.Comparator epComp = new Util.Comparator() {
00186       public int compare(Object oa, Object ob) throws ClassCastException {
00187         ExportPkg a = (ExportPkg)oa;
00188         ExportPkg b = (ExportPkg)ob;
00189         int d = a.version.compareTo(b.version);
00190         if (d == 0) {
00191           long ld = b.bpkgs.bundle.id - a.bpkgs.bundle.id;
00192           if (ld < 0)
00193             d = -1;
00194           else if (ld > 0)
00195             d = 1;
00196         }
00197         return d;
00198       }
00199     };
00200 
00201   static final Util.Comparator ipComp = new Util.Comparator() {
00212       public int compare(Object oa, Object ob) throws ClassCastException {
00213         ImportPkg a = (ImportPkg)oa;
00214         ImportPkg b = (ImportPkg)ob;
00215         int d = a.packageRange.compareTo(b.packageRange);
00216         if (d == 0) {
00217           long ld = b.bpkgs.bundle.id - a.bpkgs.bundle.id;
00218           if (ld < 0)
00219             d = -1;
00220           else if (ld > 0)
00221             d = 1;
00222         }
00223         return d;
00224       }
00225     };
00226 
00227 
00228 }

Generated on Mon Jan 11 21:19:16 2010 for OpenMobileIS by  doxygen 1.5.4