Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2014, Red Hat, Inc., and individual contributors
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * Licensed 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.jboss.weld.serialization;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 
 
The index holds identifiers for the specified set of beans (note that only instances of org.jboss.weld.bean.CommonBean and implementations of javax.enterprise.inject.spi.PassivationCapable are included). Identifiers are sorted into ascending order, according to the org.jboss.weld.serialization.spi.BeanIdentifier.asString() natural ordering.

Author(s):
Martin Kouba
 
 public class BeanIdentifierIndex implements Service {
 
     private volatile BeanIdentifier[] index;
 
     private volatile Map<BeanIdentifierIntegerreverseIndex;

    

Parameters:
identifier
Returns:
the position for the given bean identifier or null if the index does not contain the given identifier
 
     public Integer getIndex(BeanIdentifier identifier) {
         checkIsBuilt();
         Preconditions.checkArgumentNotNull(identifier"identifier");
         return .get(identifier);
     }

    

Parameters:
idx
Returns:
the identifier at the specified position
 
     public BeanIdentifier getIdentifier(int idx) {
         checkIsBuilt();
         if (idx < 0 || idx >= .) {
             throw ..unableToGetBeanIdentifier(idxthis);
         }
         return [idx];
     }

    
Note that the index can only be built once.

Parameters:
beans The set of beans the index should be built from, only instances of org.jboss.weld.bean.CommonBean and implementations of javax.enterprise.inject.spi.PassivationCapable are included
Throws:
org.jboss.weld.exceptions.IllegalStateException If the index is built already
 
     public void build(Set<Bean<?>> beans) {
 
         if (isBuilt()) {
             throw new IllegalStateException("BeanIdentifier index is already built!");
         }
 
         if (beans.isEmpty()) {
              = new BeanIdentifier[0];
              = Collections.emptyMap();
             return;
         }
 
         List<BeanIdentifiertempIndex = new ArrayList<BeanIdentifier>(beans.size());
 
         for (Bean<?> bean : beans) {
             if (bean instanceof CommonBean<?>) {
                 tempIndex.add(((CommonBean<?>) bean).getIdentifier());
            } else if (bean instanceof PassivationCapable) {
                tempIndex.add(new StringBeanIdentifier(((PassivationCapablebean).getId()));
            }
        }
        Collections.sort(tempIndexnew Comparator<BeanIdentifier>() {
            @Override
            public int compare(BeanIdentifier o1BeanIdentifier o2) {
                return o1.asString().compareTo(o2.asString());
            }
        });
         = tempIndex.toArray(new BeanIdentifier[tempIndex.size()]);
        ImmutableMap.Builder<BeanIdentifierIntegerbuilder = ImmutableMap.builder();
        for (int i = 0; i < .i++) {
            builder.put([i], i);
        }
         = builder.build();
    }

    

Returns:
true if the index is built, false otherwise
    public boolean isBuilt() {
        return  != null;
    }
    @Override
    public void cleanup() {
         = null;
    }
    private void checkIsBuilt() {
        if (!isBuilt()) {
            throw new IllegalStateException("BeanIdentifier index not built!");
        }
    }
    @Override
    public String toString() {
        return String.format("BeanIdentifierIndex [index=%s]", Arrays.toString());
    }
New to GrepCode? Check out our FAQ X