1 /*
   2  * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.corba.se.impl.ior;
  27 
  28 import java.util.ArrayList ;
  29 import java.util.Iterator ;
  30 
  31 import org.omg.CORBA_2_3.portable.InputStream ;
  32 import org.omg.CORBA_2_3.portable.OutputStream ;
  33 
  34 import com.sun.corba.se.spi.ior.IORTemplate ;
  35 import com.sun.corba.se.spi.ior.IORTemplateList ;
  36 import com.sun.corba.se.spi.ior.ObjectId ;
  37 import com.sun.corba.se.spi.ior.IORTemplate ;
  38 import com.sun.corba.se.spi.ior.IOR ;
  39 import com.sun.corba.se.spi.ior.IORFactory ;
  40 import com.sun.corba.se.spi.ior.IORFactories ;
  41 
  42 import com.sun.corba.se.spi.orb.ORB ;
  43 
  44 public class IORTemplateListImpl extends FreezableList implements IORTemplateList
  45 {
  46     /* This class must override add( int, Object ) and set( int, Object )
  47      * so that adding an IORTemplateList to an IORTemplateList just results
  48      * in a list of TaggedProfileTemplates.
  49      */
  50     public Object set( int index, Object element )
  51     {
  52         if (element instanceof IORTemplate) {
  53             return super.set( index, element ) ;
  54         } else if (element instanceof IORTemplateList) {
  55             Object result = remove( index ) ;
  56             add( index, element ) ;
  57             return result ;
  58         } else
  59             throw new IllegalArgumentException() ;
  60     }
  61 
  62     public void add( int index, Object element )
  63     {
  64         if (element instanceof IORTemplate) {
  65             super.add( index, element ) ;
  66         } else if (element instanceof IORTemplateList) {
  67             IORTemplateList tl = (IORTemplateList)element ;
  68             addAll( index, tl ) ;
  69         } else
  70             throw new IllegalArgumentException() ;
  71     }
  72 
  73     public IORTemplateListImpl()
  74     {
  75         super( new ArrayList() ) ;
  76     }
  77 
  78     public IORTemplateListImpl( InputStream is )
  79     {
  80         this() ;
  81         int size = is.read_long() ;
  82         for (int ctr=0; ctr<size; ctr++) {
  83             IORTemplate iortemp = IORFactories.makeIORTemplate( is ) ;
  84             add( iortemp ) ;
  85         }
  86 
  87         makeImmutable() ;
  88     }
  89 
  90     public void makeImmutable()
  91     {
  92         makeElementsImmutable() ;
  93         super.makeImmutable() ;
  94     }
  95 
  96     public void write( OutputStream os )
  97     {
  98         os.write_long( size() ) ;
  99         Iterator iter = iterator() ;
 100         while (iter.hasNext()) {
 101             IORTemplate iortemp = (IORTemplate)(iter.next()) ;
 102             iortemp.write( os ) ;
 103         }
 104     }
 105 
 106     public IOR makeIOR( ORB orb, String typeid, ObjectId oid )
 107     {
 108         return new IORImpl( orb, typeid, this, oid ) ;
 109     }
 110 
 111     public boolean isEquivalent( IORFactory other )
 112     {
 113         if (!(other instanceof IORTemplateList))
 114             return false ;
 115 
 116         IORTemplateList list = (IORTemplateList)other ;
 117 
 118         Iterator thisIterator = iterator() ;
 119         Iterator listIterator = list.iterator() ;
 120         while (thisIterator.hasNext() && listIterator.hasNext()) {
 121             IORTemplate thisTemplate = (IORTemplate)thisIterator.next() ;
 122             IORTemplate listTemplate = (IORTemplate)listIterator.next() ;
 123             if (!thisTemplate.isEquivalent( listTemplate ))
 124                 return false ;
 125         }
 126 
 127         return thisIterator.hasNext() == listIterator.hasNext() ;
 128     }
 129 }