Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.util » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements. See the NOTICE file distributed with this
    4    * work for additional information regarding copyright ownership. The ASF
    5    * licenses this file to You under the Apache License, Version 2.0 (the
    6    * "License"); you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
    9    * or agreed to in writing, software distributed under the License is
   10    * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   11    * KIND, either express or implied. See the License for the specific language
   12    * governing permissions and limitations under the License.
   13    */
   14   package org.apache.webbeans.util;
   15   
   16   public final class ArrayUtil
   17   {
   18   
   19       private ArrayUtil()
   20       {
   21   
   22       }
   23   
   24       /**
   25        * Compare two arrays regardless of the position of the elements
   26        * in the arrays. The complex handling with temporary flags is necessary due
   27        * to the possibility of having multiple occurrences of the same element in
   28        * the arrays. In this case both arrays have to contain the exactly same
   29        * amount of those elements. This is only suited for smaller arrays (e.g.
   30        * count < 100) since the algorithm uses a product of both arrays. If one
   31        * likes to use this for larger arrays, we'd have to use hashes.
   32        * 
   33        * @param arr1
   34        * @param arr2
   35        * @return
   36        */
   37       public static boolean equalsIgnorePosition(Object[] arr1, Object[] arr2)
   38       {
   39           if (arr1 == null && arr2 == null)
   40           {
   41               return true;
   42           }
   43   
   44           if (arr1 == null || arr2 == null)
   45           {
   46               return false;
   47           }
   48   
   49           if (arr1.length != arr2.length)
   50           {
   51               return false;
   52           }
   53   
   54           boolean[] found1 = new boolean[arr1.length];
   55           boolean[] found2 = new boolean[arr2.length];
   56   
   57           for (int i1 = 0; i1 < arr1.length; i1++)
   58           {
   59               Object o1 = arr1[i1];
   60   
   61               for (int i2 = 0; i2 < arr2.length; i2++)
   62               {
   63                   Object o2 = arr2[i2];
   64   
   65                   // if they are equal and not found already
   66                   if (o1.equals(o2) && found2[i2] == false)
   67                   {
   68                       // mark the entries in both arrays as found
   69                       found1[i1] = true;
   70                       found2[i2] = true;
   71                       break;
   72                   }
   73               }
   74           }
   75   
   76           for (int i = 0; i < found1.length; i++)
   77           {
   78               if (!found1[i] || !found2[i])
   79               {
   80                   return false;
   81               }
   82           }
   83           return true;
   84       }
   85   
   86   }

Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.util » [javadoc | source]