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 {
 
     private static final Filter[] EMPTY_FILTER_ARRAY = new Filter[0];
 
     private SortedSet<FilterListEntryfilterList;
 
     private Filter[] filters = ;
 
     }
 
     public synchronized Filter addFilter(final Filter filter,
             final Long filterIdfinal int orderfinal String orderSource) {
         if ( == null) {
              = new TreeSet<FilterListEntry>();
         }
         .add(new FilterListEntry(filterfilterIdorderorderSource));
          = this.getFilterArray();
         return filter;
     }
 
     public synchronized boolean removeFilterById(final Object filterId) {
         if ( != null) {
             for (Iterator<FilterListEntryfi = .iterator(); fi.hasNext();) {
                 FilterListEntry test = fi.next();
                 if (test.getFilterId() == filterId
                     || (test.getFilterId() != null && test.getFilterId().equals(
                         filterId))) {
                     fi.remove();
                      = this.getFilterArray();
                     return true;
                 }
             }
         }
 
         // no removed filter
         return false;
     }

    
Returns the list of Filters added to this instance or null if no filters have been added. This method doesn't need to be synced as it is called from synced methods.
 
     private Filter[] getFilterArray() {
         if ( != null && !.isEmpty()) {
             final Filter[] tmp = new Filter[.size()];
             int i = 0;
             for (FilterListEntry entry : ) {
                 tmp[i] = entry.getFilter();
                 i++;
             }
             return tmp;
         }
         return ;
     }

    
Returns the list of Filters added to this instance or null if no filters have been added. This method doesn't need to be synced as it is only returned the current cached filter array.
 
     public Filter[] getFilters() {
         return this.;
     }

    
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;
        private final String orderSource;
        FilterListEntry(final Filter filterfinal Long filterIdfinal int orderfinal String orderSource) {
            this. = filter;
            this. = filterId;
            this. = order;
            this. = orderSource;
        }
        public Filter getFilter() {
            return ;
        }
        public Long getFilterId() {
            return ;
        }
        public int getOrder() {
            return ;
        }
        public String getOrderSource() {
            return ;
        }

        
Note: this class has a natural ordering that is inconsistent with equals.
        public int compareTo(final FilterListEntry other) {
            if (this == other || equals(other)) {
                return 0;
            }
            // new service.ranking order (correct)
            if ( > other.order) {
                return -1;
            } else if ( < other.order) {
                return 1;
            }
            // compare filter id (service id)
            if ( != null && other.filterId != null) {
                int comp = .compareTo(other.filterId);
                if (comp != 0) {
                    return comp;
                }
            }
            // consider equal ranking
            return 0;
        }
        @Override
        public int hashCode() {
            if (  == null ) {
                return 0;
            }
            return .hashCode();
        }
        @Override
        public boolean equals(final Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj instanceof FilterListEntry) {
                final FilterListEntry other = (FilterListEntryobj;
                return getFilter().equals(other.getFilter());
            }
            return false;
        }
    }
New to GrepCode? Check out our FAQ X