public void orderBefore()

in src/main/java/org/apache/sling/resource/collection/impl/ResourceCollectionImpl.java [252:295]


    public void orderBefore(Resource srcResource, Resource destResource) {
		if (srcResource == null) {
			throw new IllegalArgumentException("Source Resource can not be null");
		}
		ModifiableValueMap vm = membersResource.adaptTo(ModifiableValueMap.class);
    	String[] order = vm.get(ResourceCollectionConstants.REFERENCES_PROP, new String[]{});
    	String srcPath = srcResource.getPath();
		int srcIndex = ArrayUtils.indexOf(order, srcPath);
    	if (srcIndex < 0) {
    		log.warn("Collection ordering failed, as there is no resource {} in collection {} for destResource",
    				srcPath, getPath());
    		return ;
    	}
		if (destResource == null) {
			//add it to the end.
			order = ArrayUtils.remove(order, srcIndex);
			order = ArrayUtils.add(order, srcPath);
		} else {
			String destPath = destResource.getPath();

			if (destPath.equals(srcPath)) {
				String message = MessageFormat.format("Collection ordering failed, as source {0} and destination {1} can not be same",
	    				srcPath, destPath);
				log.error(message);
				throw new IllegalArgumentException(message);
			}

			int destIndex = ArrayUtils.indexOf(order, destPath);

			if (destIndex < 0) {
				log.warn("Collection ordering failed, as there is no resource {} in collection {} for destResource",
						destPath, getPath());
				return;
			}

			order = ArrayUtils.remove(order, srcIndex);
			if (srcIndex < destIndex) { //recalculate dest index
				destIndex = ArrayUtils.indexOf(order, destPath);
			}
			order = ArrayUtils.add(order, destIndex, srcPath);
		}

		vm.put(ResourceCollectionConstants.REFERENCES_PROP, order);
	}