一、前言
关于jini-core(2.0_1)源码包中net.jini.core.constraint.Constraint约束类,对数组去空(trim)、数组hash值、数组是否相等(equal)、数组包含(contains)、数组转字符串(toString)、class校验处理等,详情源码说明。
二、源码说明
package net.jini.core.constraint;@b@@b@import java.io.InvalidObjectException;@b@import java.lang.reflect.Array;@b@import java.lang.reflect.Modifier;@b@import java.security.Principal;@b@import java.util.Arrays;@b@import java.util.Collection;@b@@b@class Constraint@b@{@b@ static Object[] trim(Object[] paramArrayOfObject, int paramInt)@b@ {@b@ if (paramInt == paramArrayOfObject.length)@b@ return paramArrayOfObject;@b@@b@ Object[] arrayOfObject = (Object[])Array.newInstance(paramArrayOfObject.getClass().getComponentType(), paramInt);@b@@b@ System.arraycopy(paramArrayOfObject, 0, arrayOfObject, 0, paramInt);@b@ return arrayOfObject;@b@ }@b@@b@ static int hash(Object[] paramArrayOfObject)@b@ {@b@ int i = 0;@b@ for (int j = paramArrayOfObject.length; --j >= 0; )@b@ i += paramArrayOfObject[j].hashCode();@b@@b@ return i;@b@ }@b@@b@ static boolean equal(Object[] paramArrayOfObject1, Object[] paramArrayOfObject2)@b@ {@b@ if (paramArrayOfObject1 == paramArrayOfObject2)@b@ return true;@b@ if (paramArrayOfObject1.length != paramArrayOfObject2.length)@b@ return false;@b@@b@ for (int i = paramArrayOfObject1.length; --i >= 0; ) {@b@ if (contains(paramArrayOfObject2, paramArrayOfObject2.length, paramArrayOfObject1[i])) break label36;@b@ label36: return false;@b@ }@b@@b@ return true;@b@ }@b@@b@ static boolean contains(Object[] paramArrayOfObject, int paramInt, Object paramObject)@b@ {@b@ while (--paramInt >= 0)@b@ if (paramObject.equals(paramArrayOfObject[paramInt]))@b@ return true;@b@@b@@b@ return false;@b@ }@b@@b@ static String toString(Object[] paramArrayOfObject)@b@ {@b@ int j;@b@ String str;@b@ if (paramArrayOfObject.length == 0)@b@ return "{}";@b@ if (paramArrayOfObject.length == 1)@b@ {@b@ if (paramArrayOfObject[0] instanceof Class)@b@ localObject = ((Class)paramArrayOfObject[0]).getName();@b@ else@b@ localObject = paramArrayOfObject[0].toString();@b@@b@ return "{" + ((String)localObject) + "}";@b@ }@b@ Object localObject = new String[paramArrayOfObject.length];@b@ int i = paramArrayOfObject.length * 2;@b@ if (paramArrayOfObject[0] instanceof Class)@b@ for (j = paramArrayOfObject.length; --j >= 0; ) {@b@ str = ((Class)paramArrayOfObject[j]).getName();@b@ localObject[j] = str;@b@ i += str.length();@b@ }@b@ else@b@ for (j = paramArrayOfObject.length; --j >= 0; ) {@b@ str = paramArrayOfObject[j].toString();@b@ localObject[j] = str;@b@ i += str.length();@b@ }@b@@b@ Arrays.sort(localObject);@b@ StringBuffer localStringBuffer = new StringBuffer(i);@b@ localStringBuffer.append("{");@b@ for (int k = 0; k < localObject.length; ++k) {@b@ if (k > 0)@b@ localStringBuffer.append(", ");@b@@b@ localStringBuffer.append(localObject[k]);@b@ }@b@ localStringBuffer.append("}");@b@ return ((String)localStringBuffer.toString());@b@ }@b@@b@ static Principal[] reduce(Collection paramCollection)@b@ {@b@ try@b@ {@b@ return reduce0((Principal[])paramCollection.toArray(new Principal[paramCollection.size()]));@b@ } catch (ArrayStoreException localArrayStoreException) {@b@ throw new IllegalArgumentException("element of collection is not a Principal");@b@ }@b@ }@b@@b@ static Principal[] reduce(Principal[] paramArrayOfPrincipal)@b@ {@b@ return reduce0((Principal[])paramArrayOfPrincipal.clone());@b@ }@b@@b@ private static Principal[] reduce0(Principal[] paramArrayOfPrincipal)@b@ {@b@ if (paramArrayOfPrincipal.length == 0) {@b@ throw new IllegalArgumentException("cannot create constraint with no elements");@b@ }@b@@b@ int i = 0;@b@ for (int j = 0; j < paramArrayOfPrincipal.length; ++j) {@b@ Principal localPrincipal = paramArrayOfPrincipal[j];@b@ if (localPrincipal == null)@b@ throw new NullPointerException("elements cannot be null");@b@@b@ if (!(contains(paramArrayOfPrincipal, i, localPrincipal)))@b@ paramArrayOfPrincipal[(i++)] = localPrincipal;@b@ }@b@@b@ return ((Principal[])trim(paramArrayOfPrincipal, i));@b@ }@b@@b@ static void verify(Principal[] paramArrayOfPrincipal)@b@ throws InvalidObjectException@b@ {@b@ if ((paramArrayOfPrincipal == null) || (paramArrayOfPrincipal.length == 0)) {@b@ throw new InvalidObjectException("cannot create constraint with no elements");@b@ }@b@@b@ for (int i = paramArrayOfPrincipal.length; --i >= 0; ) {@b@ Principal localPrincipal = paramArrayOfPrincipal[i];@b@ if (localPrincipal == null)@b@ throw new InvalidObjectException("elements cannot be null");@b@@b@ if (!(contains(paramArrayOfPrincipal, i, localPrincipal))) break label62;@b@ label62: throw new InvalidObjectException("cannot create constraint with duplicate elements");@b@ }@b@ }@b@@b@ static Class[] reduce(Collection paramCollection, boolean paramBoolean)@b@ {@b@ try@b@ {@b@ return reduce0((Class[])paramCollection.toArray(new Class[paramCollection.size()]), paramBoolean);@b@ }@b@ catch (ArrayStoreException localArrayStoreException) {@b@ throw new IllegalArgumentException("element of collection is not a Class");@b@ }@b@ }@b@@b@ static Class[] reduce(Class[] paramArrayOfClass, boolean paramBoolean)@b@ {@b@ return reduce0((Class[])paramArrayOfClass.clone(), paramBoolean);@b@ }@b@@b@ private static Class[] reduce0(Class[] paramArrayOfClass, boolean paramBoolean)@b@ {@b@ if (paramArrayOfClass.length == 0) {@b@ throw new IllegalArgumentException("cannot create constraint with no elements");@b@ }@b@@b@ int i = 0;@b@@b@ for (int j = 0; j < paramArrayOfClass.length; ++j) {@b@ Class localClass1 = paramArrayOfClass[j];@b@ verify(localClass1);@b@ for (int k = i; --k >= 0; ) {@b@ Class localClass2 = paramArrayOfClass[k];@b@ if (paramBoolean) { if (!(localClass2.isAssignableFrom(localClass1))) break label74; break label127: } if (localClass1.isAssignableFrom(localClass2))@b@ {@b@ break label127:@b@ }@b@ if (paramBoolean) label74: if (!(localClass1.isAssignableFrom(localClass2))) break label111; @b@ else if (!(localClass2.isAssignableFrom(localClass1)))@b@ break label111;@b@@b@ label111: paramArrayOfClass[k] = paramArrayOfClass[(--i)];@b@ }@b@@b@ label127: paramArrayOfClass[(i++)] = localClass1;@b@ }@b@ return ((Class[])trim(paramArrayOfClass, i));@b@ }@b@@b@ static void verify(Class paramClass)@b@ {@b@ if (paramClass == null)@b@ throw new NullPointerException("elements cannot be null");@b@@b@ if ((paramClass.isArray()) || (paramClass.isPrimitive()) || ((Modifier.isFinal(paramClass.getModifiers())) && (!(Principal.class.isAssignableFrom(paramClass)))))@b@ {@b@ throw new IllegalArgumentException("invalid class");@b@ }@b@ }@b@@b@ static void verify(Class[] paramArrayOfClass)@b@ throws InvalidObjectException@b@ {@b@ if ((paramArrayOfClass == null) || (paramArrayOfClass.length == 0)) {@b@ throw new InvalidObjectException("cannot create constraint with no elements");@b@ }@b@@b@ int i = paramArrayOfClass.length; while (--i >= 0) {@b@ Class localClass1 = paramArrayOfClass[i];@b@ if (localClass1 == null)@b@ throw new InvalidObjectException("elements cannot be null");@b@@b@ if ((localClass1.isArray()) || (localClass1.isPrimitive()) || ((Modifier.isFinal(localClass1.getModifiers())) && (!(Principal.class.isAssignableFrom(localClass1)))))@b@ {@b@ throw new InvalidObjectException("invalid class");@b@ }@b@ for (int j = i; --j >= 0; ) {@b@ Class localClass2 = paramArrayOfClass[j];@b@ if ((!(localClass1.isAssignableFrom(localClass2))) && (!(localClass2.isAssignableFrom(localClass1)))) break label143;@b@ label143: throw new InvalidObjectException("cannot create constraint with redundant elements");@b@ }@b@ }@b@ }@b@}