Example usage for com.google.common.collect Maps newEnumMap

List of usage examples for com.google.common.collect Maps newEnumMap

Introduction

In this page you can find the example usage for com.google.common.collect Maps newEnumMap.

Prototype

public static <K extends Enum<K>, V> EnumMap<K, V> newEnumMap(Map<K, ? extends V> map) 

Source Link

Document

Creates an EnumMap with the same mappings as the specified map.

Usage

From source file:com.ardor3d.extension.model.collada.jdom.ColladaAnimUtils.java

/**
 * Merge all animation channels into Ardor jointchannels
 * /*from w w  w  .  j a  v a2 s  . c  o m*/
 * @param entry
 */
@SuppressWarnings("unchecked")
private void buildAnimations(final Element parentElement, final Collection<TargetChannel> targetList) {

    final List<Element> elementTransforms = new ArrayList<Element>();
    for (final Element child : parentElement.getChildren()) {
        if (_dataCache.getTransformTypes().contains(child.getName())) {
            elementTransforms.add(child);
        }
    }
    final List<TransformElement> transformList = getNodeTransformList(elementTransforms);

    AnimationItem animationItemRoot = null;
    for (final TargetChannel targetChannel : targetList) {
        if (animationItemRoot == null) {
            animationItemRoot = targetChannel.animationItemRoot;
        }
        final String source = targetChannel.source;
        // final Target target = targetChannel.target;
        final Element targetNode = targetChannel.targetNode;

        final int targetIndex = elementTransforms.indexOf(targetNode);
        if (logger.isLoggable(Level.FINE)) {
            logger.fine(parentElement.getName() + "(" + parentElement.getAttributeValue("name") + ") -> "
                    + targetNode.getName() + "(" + targetIndex + ")");
        }

        final EnumMap<Type, ColladaInputPipe> pipes = Maps.newEnumMap(Type.class);

        final Element samplerElement = _colladaDOMUtil.findTargetWithId(source);
        for (final Element inputElement : samplerElement.getChildren("input")) {
            final ColladaInputPipe pipe = new ColladaInputPipe(_colladaDOMUtil, inputElement);
            pipes.put(pipe.getType(), pipe);
        }

        // get input (which is TIME for now)
        final ColladaInputPipe inputPipe = pipes.get(Type.INPUT);
        final ColladaInputPipe.SourceData sdIn = inputPipe.getSourceData();
        final float[] time = sdIn.floatArray;
        targetChannel.time = time;
        if (logger.isLoggable(Level.FINE)) {
            logger.fine("inputPipe: " + Arrays.toString(time));
        }

        // get output data
        final ColladaInputPipe outputPipe = pipes.get(Type.OUTPUT);
        final ColladaInputPipe.SourceData sdOut = outputPipe.getSourceData();
        final float[] animationData = sdOut.floatArray;
        targetChannel.animationData = animationData;
        if (logger.isLoggable(Level.FINE)) {
            logger.fine("outputPipe: " + Arrays.toString(animationData));
        }

        // TODO: Need to add support for other interpolation types.

        // get target array from transform list
        final TransformElement transformElement = transformList.get(targetIndex);
        final double[] array = transformElement.getArray();
        targetChannel.array = array;

        final int stride = sdOut.stride;
        targetChannel.stride = stride;

        targetChannel.currentPos = 0;
    }

    final List<Float> finalTimeList = Lists.newArrayList();
    final List<Transform> finalTransformList = Lists.newArrayList();
    final List<TargetChannel> workingChannels = Lists.newArrayList();
    for (;;) {
        float lowestTime = Float.MAX_VALUE;
        boolean found = false;
        for (final TargetChannel targetChannel : targetList) {
            if (targetChannel.currentPos < targetChannel.time.length) {
                final float time = targetChannel.time[targetChannel.currentPos];
                if (time < lowestTime) {
                    lowestTime = time;
                }
                found = true;
            }
        }
        if (!found) {
            break;
        }

        workingChannels.clear();
        for (final TargetChannel targetChannel : targetList) {
            if (targetChannel.currentPos < targetChannel.time.length) {
                final float time = targetChannel.time[targetChannel.currentPos];
                if (time == lowestTime) {
                    workingChannels.add(targetChannel);
                }
            }
        }

        for (final TargetChannel targetChannel : workingChannels) {
            final Target target = targetChannel.target;
            final float[] animationData = targetChannel.animationData;
            final double[] array = targetChannel.array;

            // set the correct values depending on accessor
            final int position = targetChannel.currentPos * targetChannel.stride;
            if (target.accessorType == AccessorType.None) {
                for (int j = 0; j < array.length; j++) {
                    array[j] = animationData[position + j];
                }
            } else {
                if (target.accessorType == AccessorType.Vector) {
                    array[target.accessorIndexX] = animationData[position];
                } else if (target.accessorType == AccessorType.Matrix) {
                    array[target.accessorIndexY * 4 + target.accessorIndexX] = animationData[position];
                }
            }
            targetChannel.currentPos++;
        }

        // bake the transform
        final Transform transform = bakeTransforms(transformList);
        finalTimeList.add(lowestTime);
        finalTransformList.add(transform);
    }

    final float[] time = new float[finalTimeList.size()];
    for (int i = 0; i < finalTimeList.size(); i++) {
        time[i] = finalTimeList.get(i);
    }
    final Transform[] transforms = finalTransformList.toArray(new Transform[finalTransformList.size()]);

    AnimationClip animationClip = animationItemRoot.getAnimationClip();
    if (animationClip == null) {
        animationClip = new AnimationClip(animationItemRoot.getName());
        animationItemRoot.setAnimationClip(animationClip);
    }

    // Make an animation channel - first find if we have a matching joint
    Joint joint = _dataCache.getElementJointMapping().get(parentElement);
    if (joint == null) {
        String nodeName = parentElement.getAttributeValue("name", (String) null);
        if (nodeName == null) { // use id if name doesn't exist
            nodeName = parentElement.getAttributeValue("id", parentElement.getName());
        }
        if (nodeName != null) {
            joint = _dataCache.getExternalJointMapping().get(nodeName);
        }

        if (joint == null) {
            // no joint still, so make a transform channel.
            final TransformChannel transformChannel = new TransformChannel(nodeName, time, transforms);
            animationClip.addChannel(transformChannel);
            _colladaStorage.getAnimationChannels().add(transformChannel);
            return;
        }
    }

    // create joint channel
    final JointChannel jointChannel = new JointChannel(joint, time, transforms);
    animationClip.addChannel(jointChannel);
    _colladaStorage.getAnimationChannels().add(jointChannel);
}

From source file:com.google.cloud.storage.StorageImpl.java

private static Map<StorageRpc.Option, ?> optionMap(Long generation, Long metaGeneration,
        Iterable<? extends Option> options, boolean useAsSource) {
    Map<StorageRpc.Option, Object> temp = Maps.newEnumMap(StorageRpc.Option.class);
    for (Option option : options) {
        Object prev = temp.put(option.getRpcOption(), option.getValue());
        checkArgument(prev == null, "Duplicate option %s", option);
    }// www  .j  a  va 2  s .c om
    Boolean value = (Boolean) temp.remove(DELIMITER);
    if (Boolean.TRUE.equals(value)) {
        temp.put(DELIMITER, PATH_DELIMITER);
    }
    if (useAsSource) {
        addToOptionMap(IF_GENERATION_MATCH, IF_SOURCE_GENERATION_MATCH, generation, temp);
        addToOptionMap(IF_GENERATION_NOT_MATCH, IF_SOURCE_GENERATION_NOT_MATCH, generation, temp);
        addToOptionMap(IF_METAGENERATION_MATCH, IF_SOURCE_METAGENERATION_MATCH, metaGeneration, temp);
        addToOptionMap(IF_METAGENERATION_NOT_MATCH, IF_SOURCE_METAGENERATION_NOT_MATCH, metaGeneration, temp);
    } else {
        addToOptionMap(IF_GENERATION_MATCH, generation, temp);
        addToOptionMap(IF_GENERATION_NOT_MATCH, generation, temp);
        addToOptionMap(IF_METAGENERATION_MATCH, metaGeneration, temp);
        addToOptionMap(IF_METAGENERATION_NOT_MATCH, metaGeneration, temp);
    }
    return ImmutableMap.copyOf(temp);
}

From source file:edu.udo.scaffoldhunter.model.db.DbManagerHibernate.java

@Override
public Map<AccumulationFunction, Double> getAccNumPropertyDataset(PropertyDefinition property)
        throws DatabaseException {
    Map<AccumulationFunction, Double> results = Maps.newEnumMap(AccumulationFunction.class);
    Session hibernateSession = null;/*from w  w  w  . j av a  2 s  .  c o  m*/

    try {
        hibernateSession = sessionFactory.getCurrentSession();
        hibernateSession.beginTransaction();

        String propertyType;
        if (property.isScaffoldProperty()) {
            propertyType = "ScaffoldNumProperty";
        } else {
            propertyType = MoleculeNumProperty.class.getName();
        }

        Query query = hibernateSession
                .createQuery("SELECT avg(prop.value), max(prop.value), min(prop.value), sum(prop.value) "
                        + "FROM " + propertyType + " prop " + "WHERE prop.type = :property");
        query.setParameter("property", property);

        Object[] r = (Object[]) query.uniqueResult();
        hibernateSession.getTransaction().commit();

        results.put(AccumulationFunction.Average, (Double) r[0]);
        results.put(AccumulationFunction.Maximum, (Double) r[1]);
        results.put(AccumulationFunction.Minimum, (Double) r[2]);
        results.put(AccumulationFunction.Sum, (Double) r[3]);

        return results;
    } catch (HibernateException ex) {
        hibernateSession.getTransaction().rollback();
        logger.error("Querying of accumulated Property failed.\n{}\n{}", ex, stacktrace(ex));
        closeAndRollBackErroneousSession(hibernateSession);
        throw new DatabaseException("Querying of accumulated Property failed", ex);
    }
}

From source file:com.google.cloud.compute.deprecated.ComputeImpl.java

private Map<ComputeRpc.Option, ?> optionMap(Option... options) {
    Map<ComputeRpc.Option, Object> optionMap = Maps.newEnumMap(ComputeRpc.Option.class);
    for (Option option : options) {
        Object prev = optionMap.put(option.getRpcOption(), option.getValue());
        checkArgument(prev == null, "Duplicate option %s", option);
    }// w w w  .j  av  a2s.com
    return optionMap;
}