Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
  *   http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
  */
 package org.apache.sling.engine.impl.filter;
 
 
The SlingFilterChainHelper class is used by Sling to support building lists of Filters. To ensure filter ordering, each filter is optionally registered with an ordering index. If none is provided the default ordering index is Integer.MAX_VALUE to append the filter to the end of the list.
 
 public class SlingFilterChainHelper {
 
 
     Filter[] filters;
 
     }
 
     public synchronized Filter addFilter(Filter filter,
             Long filterIdint order) {
          = null;
         if ( == null) {
              = new TreeSet<FilterListEntry>();
         }
         .add(new FilterListEntry(filterfilterIdorder));
         return filter;
     }
 
     public synchronized Filter[] removeAllFilters() {
         // will be returned after cleaning the lists
         Filter[] removedFilters = getFilters();
 
          = null;
          = null;
 
         return removedFilters;
     }
 
     public synchronized Filter removeFilter(Filter filter) {
         if ( != null) {
              = null;
             for (Iterator<FilterListEntryfi = .iterator(); fi.hasNext();) {
                 FilterListEntry test = fi.next();
                 if (test.getFilter().equals(filter)) {
                     fi.remove();
                     return test.getFilter();
                 }
             }
         }
 
         // no removed ComponentFilter
         return null;
     }
 
     public synchronized boolean removeFilterById(Object filterId) {
         if ( != null) {
              = null;
             for (Iterator<FilterListEntryfi = .iterator(); fi.hasNext();) {
                 FilterListEntry test = fi.next();
                 if (test.getFitlerId() == filterId
                     || (test.getFitlerId() != null && test.getFitlerId().equals(
                         filterId))) {
                     fi.remove();
                     return true;
                 }
             }
         }
 
         // no removed ComponentFilter
         return false;
     }

    
Returns the list of Filters added to this instance or null if no filters have been added.
    public synchronized Filter[] getFilters() {
        if ( == null) {
            if ( != null && !.isEmpty()) {
                Filter[] tmp = new Filter[.size()];
                int i = 0;
                for (FilterListEntry entry : ) {
                    tmp[i] = entry.getFilter();
                    i++;
                }
                 = tmp;
            }
        }
        return ;
    }

    
Returns the list of FilterListEntrys added to this instance or null if no filters have been added.
    public synchronized FilterListEntry[] getFilterListEntries() {
        FilterListEntry[] result = null;
        if ( != null && !.isEmpty()) {
            result = new FilterListEntry[.size()];
            .toArray(result);
        }
        return result;
    }
    public static class FilterListEntry implements Comparable<FilterListEntry> {
        private final Filter filter;
        private final Long filterId;
        private final int order;
        FilterListEntry(Filter filterLong filterIdint order) {
            this. = filter;
            this. = filterId;
            this. = order;
        }
        public Filter getFilter() {
            return ;
        }
        public Long getFitlerId() {
            return ;
        }
        public int getOrder() {
            return ;
        }

        
Note: this class has a natural ordering that is inconsistent with equals.
        public int compareTo(FilterListEntry other) {
            if (this == other || equals(other)) {
                return 0;
            }
            if ( < other.order) {
                return -1;
            } else if ( > other.order) {
                return 1;
            }
            // if the filterId is comparable and the other is of the same class
            if ( != null && other.filterId != null) {
                int comp = .compareTo(other.filterId);
                if (comp != 0) {
                    return comp;
                }
            }
            // this is inserted, obj is existing key
            return 1; // insert after current key
        }
        @Override
        public int hashCode() {
            if (  == null ) {
                return 0;
            }
            return .hashCode();
        }
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj instanceof FilterListEntry) {
                FilterListEntry other = (FilterListEntryobj;
                return getFilter().equals(other.getFilter());
            }
            return false;
        }
    }
New to GrepCode? Check out our FAQ X