home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / TRIAL / JBUILDER / JVISBRKR.Z / ListServer.java < prev    next >
Encoding:
Java Source  |  1998-05-08  |  2.5 KB  |  93 lines

  1. /*
  2.  * Copyright (c) 1997-1998 Borland International, Inc. All Rights Reserved.
  3.  * 
  4.  * This SOURCE CODE FILE, which has been provided by Borland as part
  5.  * of a Borland product for use ONLY by licensed users of the product,
  6.  * includes CONFIDENTIAL and PROPRIETARY information of Borland.  
  7.  *
  8.  * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS 
  9.  * OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
  10.  * THE PRODUCT.
  11.  *
  12.  * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND, ITS RELATED
  13.  * COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS
  14.  * OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR DISTRIBUTION
  15.  * OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES ARISING OUT OF
  16.  * OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF PROGRAMS
  17.  * OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE
  18.  * CODE FILE.
  19.  */
  20. package visibroker.samples.pass_by_value.list;
  21.  
  22. public class ListServer extends _ListUtilityImplBase {
  23.  
  24.   ListServer(String name) {
  25.     super(name);
  26.   }
  27.  
  28.   public int length(List list) {
  29.     int result = 0;
  30.     for( ; list != null; list = list.next()) {
  31.       result++;
  32.     }
  33.     return result;
  34.   }
  35.       
  36.   public List reverse(List list) {
  37.     List result = null;
  38.     for( ; list != null; list = list.next()) {
  39.       result = new List(list.data(), result);
  40.     }
  41.     return result;
  42.   }
  43.  
  44.   public List sort(List list) {
  45.     int length = length(list);
  46.     if(length <= 1) {
  47.       return list;
  48.     }
  49.     // split the list in half
  50.     List lhs = list;
  51.     for(int i = 0; i < length / 2 - 1; i++) {
  52.       list = list.next();
  53.     }
  54.     List rhs = list.next();
  55.     // this actually splits the lists
  56.     list.next(null);
  57.     // sort and merge the two halves
  58.     return merge(sort(lhs), sort(rhs));
  59.   }
  60.  
  61.   public List merge(List lhs, List rhs) {
  62.     if(lhs == null) {
  63.       return rhs;
  64.     }
  65.     if(rhs == null) {
  66.       return lhs;
  67.     }
  68.     // figure out which side is next
  69.     List head;
  70.     if(lhs.data().compareTo(rhs.data()) < 0) {
  71.       head = lhs;
  72.       lhs = lhs.next();
  73.     }
  74.     else {
  75.       head = rhs;
  76.       rhs = rhs.next();
  77.     }
  78.     head.next(merge(lhs, rhs));
  79.     return head;
  80.   }
  81.  
  82.   public static void main(String[] args) {
  83.     org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null);
  84.     org.omg.CORBA.BOA boa = orb.BOA_init();
  85.     ListUtility impl = new ListServer("demo");
  86.     boa.obj_is_ready(impl);
  87.     System.out.println(impl + " is ready.");
  88.     boa.impl_is_ready();
  89.   }
  90.  
  91. }
  92.  
  93.