/** * Checks whether a type is an array. The canBeConstant flag allows or disallows it to be an array * of constant values. * * @param t Type * @param canBeConstant Can the array be constant? * @return Is it an array */ public static boolean isArray(Type t, boolean canBeConstant) { if (!canBeConstant) { return t.equals(Type.ArrayChar) || t.equals(Type.ArrayInt); } return isArray(t, false) || t.equals(Type.ConstArrayChar) || t.equals(Type.ConstArrayInt); }
/** * Returns a string version of the commodity type. * * <p>This method is actually unnecessary; if you just try to use Commodity.Type.WALNUT as a * string, you get the string "WALNUT" -- this is a property of Enums. * * @param t the commodity type you would like to turn into a string * @return the string version of the commodity type */ public static String toString(Type t) { if (t.equals(Type.WALNUT)) return "Walnut"; else if (t.equals(Type.PECAN)) return "Pecan"; else if (t.equals(Type.ALMOND)) return "Almond"; else if (t.equals(Type.CASHEW)) return "Cashew"; else return ""; }
/** * Converts a non-const value to a const-value * * @param t Non-const value X * @return const X or None if it cannot be done */ public static Type toConst(Type t) { if (t.equals(Type.Char)) return Type.ConstChar; else if (t.equals(Type.Int)) return Type.ConstInt; else if (t.equals(Type.ArrayChar)) return Type.ConstArrayChar; else if (t.equals(Type.ArrayInt)) return Type.ConstArrayInt; return Type.None; }
/** * Removes an item. * * @param item The item to remove. * @param preferredSlot The preferred slot. * @param allowZero If a zero amount item should be allowed. * @return The number of items removed. */ public int remove(Item item, int preferredSlot, boolean allowZero) { int removed = 0; if ((item.getDefinition().isStackable() || type.equals(Type.ALWAYS_STACK)) && !type.equals(Type.NEVER_STACK)) { int slot = getSlotById(item.getId()); Item stack = get(slot); if (stack.getCount() > item.getCount()) { removed = item.getCount(); set(slot, new Item(stack.getId(), stack.getCount() - item.getCount())); } else { removed = stack.getCount(); set(slot, allowZero ? new Item(stack.getId(), 0) : null); } } else { for (int i = 0; i < item.getCount(); i++) { int slot = getSlotById(item.getId()); if (i == 0 && preferredSlot != -1) { Item inSlot = get(preferredSlot); if (inSlot.getId() == item.getId()) { slot = preferredSlot; } } if (slot != -1) { removed++; set(slot, null); } else { break; } } } return removed; }
/** * Checks whether a type is const. * * @param t Type * @param isArray Is it an array or not * @return It does the checking */ public static boolean isConst(Type t, boolean isArray) { if (isArray) { return t.equals(Type.ConstArrayChar) || t.equals(Type.ConstArrayInt); } return t.equals(Type.ConstChar) || t.equals(Type.ConstInt); }
/** Symbolically executes the corresponding Java Virtual Machine instruction. */ public void visitINVOKESPECIAL(INVOKESPECIAL o) { if (o.getMethodName(cpg).equals(Constants.CONSTRUCTOR_NAME)) { UninitializedObjectType t = (UninitializedObjectType) stack().peek(o.getArgumentTypes(cpg).length); if (t == Frame._this) { Frame._this = null; } stack().initializeObject(t); locals().initializeObject(t); } stack().pop(); // objectref for (int i = 0; i < o.getArgumentTypes(cpg).length; i++) { stack().pop(); } // We are sure the invoked method will xRETURN eventually // We simulate xRETURNs functionality here because we // don't really "jump into" and simulate the invoked // method. if (o.getReturnType(cpg) != Type.VOID) { Type t = o.getReturnType(cpg); if (t.equals(Type.BOOLEAN) || t.equals(Type.CHAR) || t.equals(Type.BYTE) || t.equals(Type.SHORT)) { t = Type.INT; } stack().push(t); } }
/** Symbolically executes the corresponding Java Virtual Machine instruction. */ public void visitGETSTATIC(GETSTATIC o) { Type t = o.getFieldType(cpg); if (t.equals(Type.BOOLEAN) || t.equals(Type.CHAR) || t.equals(Type.BYTE) || t.equals(Type.SHORT)) { t = Type.INT; } stack().push(t); }
/** * Attempts to add a specific slot. * * @param item The item. * @param slot The desired slot. * @return <code>true</code> if the item was added, <code>false</code> if not. */ public boolean add(Item item, int slot) { if (item == null) return false; int newSlot = (slot > -1) ? slot : freeSlot(); if ((item.getDefinition().isStackable() || type.equals(Type.ALWAYS_STACK)) && !type.equals(Type.NEVER_STACK)) { if (getCount(item.getId()) > 0) { newSlot = getSlotById(item.getId()); } } if (newSlot == -1) { // the free slot is -1 return false; } if (get(newSlot) != null) { newSlot = freeSlot(); } if ((item.getDefinition().isStackable() || type.equals(Type.ALWAYS_STACK)) && !type.equals(Type.NEVER_STACK)) { for (int i = 0; i < items.length; i++) { if (items[i] != null && items[i].getId() == item.getId()) { int totalCount = item.getCount() + items[i].getCount(); if (totalCount >= Constants.MAX_ITEMS || totalCount < 1) { return false; } set(i, new Item(items[i].getId(), items[i].getCount() + item.getCount())); return true; } } if (newSlot == -1) { return false; } else { set(slot > -1 ? newSlot : freeSlot(), item); return true; } } else { int slots = freeSlots(); if (slots >= item.getCount()) { boolean b = firingEvents; firingEvents = false; try { for (int i = 0; i < item.getCount(); i++) { set(slot > -1 ? newSlot : freeSlot(), new Item(item.getId())); } if (b) { fireItemsChanged(); } return true; } finally { firingEvents = b; } } else { return false; } } }
private static Class<?> getClosureInterface(Type type) { if (!type.isReference()) { if (type.equals(Type.VOID)) { return VoidClosure.class; } else if (type.equals(Type.BOOLEAN)) { return BooleanClosure.class; } else if (type.equals(Type.INT)) { return IntegerClosure.class; } else if (type.equals(Type.LONG)) { return LongClosure.class; } else if (type.equals(Type.FLOAT)) { return FloatClosure.class; } else if (type.equals(Type.DOUBLE)) { return DoubleClosure.class; } else if (type.equals(Type.BYTE)) { return ByteClosure.class; } else if (type.equals(Type.CHAR)) { return CharacterClosure.class; } else if (type.equals(Type.SHORT)) { return ShortClosure.class; } throw new RuntimeException("Non-exhaustive matching!"); } else { return ObjectClosure.class; } }
/** Symbolically executes the corresponding Java Virtual Machine instruction. */ public void visitGETFIELD(GETFIELD o) { stack().pop(); Type t = o.getFieldType(cpg); if (t.equals(Type.BOOLEAN) || t.equals(Type.CHAR) || t.equals(Type.BYTE) || t.equals(Type.SHORT)) { t = Type.INT; } stack().push(t); }
@Override public boolean equals(final Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; if (!super.equals(o)) return false; final TypeApp typeApp = (TypeApp) o; if (!base.equals(typeApp.base)) return false; if (!arg.equals(typeApp.arg)) return false; return true; }
/** * Lists all possible casts from a type, explicit or just implicit, depending on the flag. * * @param type Type * @param explicit Does it include explicit casts? * @return List of possible casts */ private static List<Type> canCastFrom(Type type, boolean explicit) { if (casts == null) { initCasts(); } List<Type> cast = new ArrayList<Type>(casts.get(type)); if ((type.equals(Type.Int) || type.equals(Type.ConstInt)) && explicit) { cast.add(Type.Char); cast.add(Type.ConstChar); } return cast; }
@Override public boolean equals(Object obj) { if (!(obj instanceof SabresDescriptor)) { return false; } if (obj == this) { return true; } SabresDescriptor other = (SabresDescriptor) obj; return type.equals(other.type) && !(ofType != null && !(ofType.equals(other.ofType))) && !(name != null && !(name.equals(other.name))); }
/** Calculate the type of this expression, using the given context and type environment. */ public Type typeOf(Context ctxt, TypeEnv locals) throws Failure { Type et = exp.typeOf(ctxt, locals); if (!et.equals(Type.INT)) { ctxt.report(new Failure("CastInt")); } return Type.DOUBLE; }
private MemoryLockToken getLock(String resource, Type type, long wait) throws InterruptedException { ReentrantReadWriteLock lockEntry; synchronized (locks) { if (locks.containsKey(resource)) { lockEntry = locks.get(resource); } else { lockEntry = new ReentrantReadWriteLock(true); locks.put(resource, lockEntry); } } Lock lock = (type.equals(Type.READ)) ? lockEntry.readLock() : lockEntry.writeLock(); if (wait == -1) { lock.lock(); } else { if (wait > 0) { if (!lock.tryLock(wait, TimeUnit.MILLISECONDS)) { return null; } } else { if (!lock.tryLock()) { return null; } } } synchronized (locks) { if (!locks.containsKey(resource)) { locks.put(resource, lockEntry); } } return new MemoryLockToken(lockEntry, lock, resource); }
/** * Attempts to add an item into the next free slot. * * @param item The item. * @return <code>true</code> if the item was added, <code>false</code> if not. */ public boolean add(Item item) { if (item.getDefinition().isStackable() || type.equals(Type.ALWAYS_STACK)) { for (int i = 0; i < items.length; i++) { if (items[i] != null && items[i].getId() == item.getId()) { int totalCount = item.getCount() + items[i].getCount(); if (totalCount >= Integer.MAX_VALUE || totalCount < 1) { return false; } set(i, new Item(items[i].getId(), items[i].getCount() + item.getCount())); return true; } } int slot = freeSlot(); if (slot == -1) { return false; } else { set(slot, item); return true; } } else { int slots = freeSlots(); if (slots >= item.getCount()) { for (int i = 0; i < item.getCount(); i++) { set(freeSlot(), new Item(item.getId())); } return true; } else { return false; } } }
@Override public String toString() { if (type.equals(Type.Pointer)) { return String.format("%s to %s", type.toString(), name); } if (type.equals(Type.List)) { if (ofType.equals(Type.Pointer)) { return String.format("List of %s to %s", ofType.toString(), name); } return String.format("List of %s", ofType.toString()); } return type.toString(); }
private static int checkParameterizedType( String subroutineName, ParameterizedType expectedType, ArgumentsList arguments, List<LispObject> args, int argsCounter, int i) { Type rawType = expectedType.getRawType(); Type expectedTypeArguments = expectedType.getActualTypeArguments()[0]; try { if (((Class) rawType).isInstance(args.get(argsCounter))) { Type actualTypeArguments = ((ParameterizedType) (Type) args.get(argsCounter).getClass()) .getActualTypeArguments()[0]; if (!expectedTypeArguments.equals(actualTypeArguments)) { throw new WrongTypeArgumentException( ((Class) rawType).getSimpleName() + "<" + ((Class) expectedTypeArguments).getSimpleName() + ">", args.get(argsCounter)); } arguments.setValue(i, args.get(argsCounter)); return argsCounter + 1; } else { if (arguments.isOptional(i)) return -1; throw new WrongTypeArgumentException(expectedType.toString(), args.get(argsCounter)); } } catch (IndexOutOfBoundsException e) { if (arguments.isOptional(i)) return -1; throw new WrongNumberOfArgumentsException(subroutineName, i); } }
/** * Handles a DataStoreEvent with the specified type. If the current event type is not equal to the * specified type, the events accumulated up to now will be fired first. * * <p>The new event will be aggregated and fired on demand if {@link #accumulateDataStoreEvents} * is set, otherwise all registered <code>DataStoreListener</code> will be notified immediately * that the content of the database has been changed. * * @param objects the objects that have been changed, i.e. inserted, deleted or updated */ private void fireObjectsChanged(DBIDs objects, Type type) { // flush first if (currentDataStoreEventType != null && !currentDataStoreEventType.equals(type)) { flushDataStoreEvents(); } if (accumulateDataStoreEvents) { if (this.dataStoreObjects == null) { this.dataStoreObjects = DBIDUtil.newHashSet(); } this.dataStoreObjects.addDBIDs(objects); currentDataStoreEventType = type; return; } // Execute immediately: DataStoreEvent e; switch (type) { case INSERT: e = DataStoreEvent.insertionEvent(objects); break; case REMOVE: e = DataStoreEvent.removalEvent(objects); break; case UPDATE: e = DataStoreEvent.updateEvent(objects); break; default: return; } for (int i = dataListenerList.size(); --i >= 0; ) { dataListenerList.get(i).contentChanged(e); } }
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } AliasRef other = (AliasRef) obj; if (name == null) { if (other.name != null) { return false; } } else if (!name.equals(other.name)) { return false; } if (type == null) { if (other.type != null) { return false; } } else if (!type.equals(other.type)) { return false; } return true; }
@Override public String toString() { if (type.equals(Type.STRING)) { return strings.toString(); } else { return "" + num; } }
/** Symbolically executes the corresponding Java Virtual Machine instruction. */ public void visitATHROW(ATHROW o) { Type t = stack().pop(); stack().clear(); if (t.equals(Type.NULL)) { stack().push(Type.getType("Ljava/lang/NullPointerException;")); } else { stack().push(t); } }
public boolean equals(Object o) { if (!(o instanceof MultiType)) return false; MultiType multi = (MultiType) o; if (resolved != null) return resolved.equals(multi.resolved); else if (multi.resolved != null) return false; return interfaces.keySet().equals(multi.interfaces.keySet()); }
/** * Return true if this type does not correspond to a single token class. This occurs if the type * is not instantiable, or it represents either an abstract base class or an interface. * * @param type The type to be checked * @return True if this type does not correspond to a single token class. */ public boolean isCompatible(Type type) { if (type.equals(BaseType.UNKNOWN)) { return true; } if (!(type instanceof ObjectType)) { return false; } return _isLessThanOrEqualTo((ObjectType) type, this); }
/** * Returns the public api representation of a folder. * * <p>Note: properties are modelled after the OpenCMIS node properties */ public Folder getFolder(NodeRef nodeRef) { Type type = getType(nodeRef); if (type.equals(Type.FOLDER)) { Properties properties = getCMISProperties(nodeRef); Folder folder = new Folder(nodeRef, properties); return folder; } else { throw new InvalidArgumentException("Node is not a folder"); } }
/** * Checks if there is room in the inventory for an item. * * @param item The item. * @return <code>true</code> if so, <code>false</code> if not. */ public boolean hasRoomFor(Item item) { if ((item.getDefinition().isStackable() || type.equals(Type.ALWAYS_STACK)) && !type.equals(Type.NEVER_STACK)) { for (int i = 0; i < items.length; i++) { if (items[i] != null && items[i].getId() == item.getId()) { int totalCount = item.getCount() + items[i].getCount(); if (totalCount >= Constants.MAX_ITEMS || totalCount < 1) { return false; } return true; } } int slot = freeSlot(); return slot != -1; } else { int slots = freeSlots(); return slots >= item.getCount(); } }
/** * Returns the public api representation of a document. * * <p>Note: properties are modelled after the OpenCMIS node properties */ public Document getDocument(NodeRef nodeRef) { Type type = getType(nodeRef); if (type.equals(Type.DOCUMENT)) { Properties properties = getCMISProperties(nodeRef); Document doc = new Document(nodeRef, properties); return doc; } else { throw new InvalidArgumentException("Node is not a file"); } }
@Override public PersonFavourite addFavourite(String userName, NodeRef nodeRef) { PersonFavourite personFavourite = null; Type type = getType(nodeRef); if (type == null) { throw new IllegalArgumentException("Cannot favourite this node"); } else if (type.equals(Type.FILE)) { personFavourite = addFavouriteDocumentOrFolder(userName, Type.FILE, nodeRef); } else if (type.equals(Type.FOLDER)) { personFavourite = addFavouriteDocumentOrFolder(userName, Type.FOLDER, nodeRef); } else if (type.equals(Type.SITE)) { personFavourite = addFavouriteSite(userName, nodeRef); } else { throw new IllegalArgumentException("Cannot favourite this node"); } return personFavourite; }
@Override public boolean removeFavourite(String userName, NodeRef nodeRef) { boolean exists = false; Type type = getType(nodeRef); if (type == null) { throw new IllegalArgumentException("Cannot un-favourite this node"); } else if (type.equals(Type.FILE)) { exists = removeFavouriteNode(userName, type, nodeRef); } else if (type.equals(Type.FOLDER)) { exists = removeFavouriteNode(userName, type, nodeRef); } else if (type.equals(Type.SITE)) { exists = removeFavouriteSite(userName, nodeRef); } else { throw new IllegalArgumentException("Cannot un-favourite this node"); } return exists; }
@Override public boolean isFavourite(String userName, NodeRef nodeRef) { boolean isFavourite = false; Type type = getType(nodeRef); if (type == null) { throw new IllegalArgumentException("Unsupported node type"); } else if (type.equals(Type.FILE)) { isFavourite = isFavouriteNode(userName, type, nodeRef); } else if (type.equals(Type.FOLDER)) { isFavourite = isFavouriteNode(userName, type, nodeRef); } else if (type.equals(Type.SITE)) { isFavourite = isFavouriteSite(userName, nodeRef); } else { throw new IllegalArgumentException("Unsupported node type"); } return isFavourite; }