Example usage for com.google.common.collect Iterables getLast

List of usage examples for com.google.common.collect Iterables getLast

Introduction

In this page you can find the example usage for com.google.common.collect Iterables getLast.

Prototype

@Nullable
public static <T> T getLast(Iterable<? extends T> iterable, @Nullable T defaultValue) 

Source Link

Document

Returns the last element of iterable or defaultValue if the iterable is empty.

Usage

From source file:ubc.pavlab.gotrack.beans.Cache.java

@PostConstruct
public void init() {
    // You can do here your initialization thing based on managed properties, if necessary.
    log.info("Cache init");

    applicationLevelDataCache = Collections.synchronizedMap(applicationLevelDataCache);

    log.info("Used Memory: "
            + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1000000 + " MB");

    // Obtain SpeciesDAO.
    SpeciesDAO speciesDAO = daoFactoryBean.getGotrack().getSpeciesDAO();
    log.info("SpeciesDAO successfully obtained: " + speciesDAO);

    speciesList = speciesDAO.list();// w  w w.  j  av a  2s  .  c om

    log.info("Species List successfully obtained: " + speciesList);

    // Obtain CacheDAO.
    CacheDAO cacheDAO = daoFactoryBean.getGotrack().getCacheDAO();
    log.info("CacheDAO successfully obtained: " + cacheDAO);

    evidenceCodeCategories = cacheDAO.getEvidenceCategories();

    // System.gc();();
    log.info("Used Memory: "
            + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1000000 + " MB");

    goSetSizes = cacheDAO.getGOSizes();

    log.info("GO Set sizes successfully obtained");

    // System.gc();
    log.info("Used Memory: "
            + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1000000 + " MB");

    List<Integer> eds = new ArrayList<Integer>(cacheDAO.getGOEditions());
    // List<List<Integer>> edPartitions = Lists.partition( eds, 20 );
    // int cnt = 0;
    // for ( List<Integer> list : edPartitions ) {
    // Map<Integer, Set<Relationship>> tmp = cacheDAO.getOntologies( list );
    // log.info( "GO Ontologies Retrieved: " + tmp.size() );
    // for ( Entry<Integer, Set<Relationship>> relsEntry : tmp.entrySet() ) {
    // ontologies.put( relsEntry.getKey(), new GeneOntology( relsEntry.getValue() ) );
    // cnt++;
    // relsEntry.getValue().clear();
    //
    // }
    // // System.gc();
    // log.info( "GO Ontologies Loaded: " + cnt + "/" + eds.size() );
    // log.info( "Used Memory: " + ( Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory() )
    // / 1000000 + " MB" );
    //
    // }

    ontologies = cacheDAO.getOntologies();

    System.gc();
    log.info("GO Ontologies Loaded: " + ontologies.keySet().size() + "/" + eds.size());
    log.info("Used Memory: "
            + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1000000 + " MB");

    aggregates = cacheDAO.getAggregates();

    log.info("Aggregates successfully obtained");

    log.info("Used Memory: "
            + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1000000 + " MB");

    // Create speciesAverage view
    for (Species species : speciesList) {
        Integer speciesId = species.getId();
        Map<Edition, StatsEntry> a = aggregates.get(speciesId);
        HashMap<Edition, Double> sa = new HashMap<Edition, Double>();
        if (a != null) {
            for (Entry<Edition, StatsEntry> editionEntry : a.entrySet()) {
                sa.put(editionEntry.getKey(), editionEntry.getValue().getAverageDirects());
            }
        }
        speciesAverage.put(speciesId, sa);
    }

    log.info("speciesAverages successfully computed");

    log.info("Used Memory: "
            + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1000000 + " MB");

    genePopulations = cacheDAO.getPopulations();

    log.info("gene populations successfully obtained");

    log.info("Used Memory: "
            + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1000000 + " MB");

    // currentEditions = cacheDAO.getCurrentEditions();
    // log.debug( "Current Editions Size: " + currentEditions.size() );

    allEditions = cacheDAO.getAllEditions();
    log.debug("All Editions Size: " + allEditions.size());

    for (Integer species : allEditions.keySet()) {
        List<Edition> l = allEditions.get(species);
        Collections.sort(l);
        Edition ed = Iterables.getLast(l, null);
        log.debug("Current edition for species_id (" + species + "): " + ed);
        currentEditions.put(species, ed);
    }

    // log.info( "Loading accession to geneSymbol cache..." );
    // for ( Species species : speciesList ) {
    // Integer speciesId = species.getId();
    // Edition currEd = currentEditions.get( speciesId );
    //
    // if ( currEd == null ) continue;
    // log.debug( species.getCommonName() + ": " + currEd.toString() );
    // // get current accessions
    // Map<String, Accession> currAccMap = cacheDAO.getAccessions( speciesId, currEd.getEdition() );
    //
    // // Create Map of current genes
    // Map<String, Gene> currentGenes = new HashMap<>();
    //
    // for ( Accession acc : currAccMap.values() ) {
    // String symbol = acc.getSymbol();
    // Gene gene = currentGenes.get( symbol.toUpperCase() );
    // if ( gene == null ) {
    // gene = new Gene( symbol );
    // currentGenes.put( symbol.toUpperCase(), gene );
    // }
    // gene.getAccessions().add( acc );
    // gene.getSynonyms().addAll( acc.getSynonyms() );
    //
    // }
    //
    // speciesToCurrentGenes.put( speciesId, currentGenes );
    //
    // log.info( "Done loading accession to geneSymbol for species (" + speciesId + "), size: "
    // + currAccMap.size() + " unique symbols: " + currentGenes.size() );
    // }
    // log.info( "Done loading accession to geneSymbol cache..." );

    speciesToCurrentGenes = cacheDAO.getCurrentGenes();
    log.info("Done loading current genes...");

    for (Species species : speciesList) {
        if (speciesToCurrentGenes.keySet().contains(species.getId())) {
            log.info("Current gene size for species (" + species + "): "
                    + speciesToCurrentGenes.get(species.getId()).size());
        }
    }

    log.info("Used Memory: "
            + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1000000 + " MB");

    log.info("Cache Completed");

}

From source file:com.spotify.asyncdatastoreclient.Key.java

/**
 * Return element kind of key, or null if not set.
 *
 * This is a shortcut for {@code Key.getPath().get(Key.getPath().size() - 1).getKind()}
 *
 * @return the element kind./*w w w  . j  a v  a  2s.c  om*/
 */
public String getKind() {
    final DatastoreV1.Key.PathElement element = Iterables.getLast(key.getPathElementList(), null);
    if (element == null) {
        return null;
    }
    return element.hasKind() ? element.getKind() : null;
}

From source file:org.apache.metron.elasticsearch.utils.ElasticsearchUtils.java

protected static List<HostnamePort> getIps(Map<String, Object> globalConfiguration) {
    Object ipObj = globalConfiguration.get("es.ip");
    Object portObj = globalConfiguration.get("es.port");
    if (ipObj == null) {
        return Collections.emptyList();
    }/*www  .  ja va2s.c  om*/
    if (ipObj instanceof String && ipObj.toString().contains(",") && ipObj.toString().contains(":")) {
        List<String> ips = Arrays.asList(((String) ipObj).split(","));
        List<HostnamePort> ret = new ArrayList<>();
        for (String ip : ips) {
            Iterable<String> tokens = Splitter.on(":").split(ip);
            String host = Iterables.getFirst(tokens, null);
            String portStr = Iterables.getLast(tokens, null);
            ret.add(new HostnamePort(host, Integer.parseInt(portStr)));
        }
        return ret;
    } else if (ipObj instanceof String && ipObj.toString().contains(",")) {
        List<String> ips = Arrays.asList(((String) ipObj).split(","));
        List<HostnamePort> ret = new ArrayList<>();
        for (String ip : ips) {
            ret.add(new HostnamePort(ip, Integer.parseInt(portObj + "")));
        }
        return ret;
    } else if (ipObj instanceof String && !ipObj.toString().contains(":")) {
        return ImmutableList.of(new HostnamePort(ipObj.toString(), Integer.parseInt(portObj + "")));
    } else if (ipObj instanceof String && ipObj.toString().contains(":")) {
        Iterable<String> tokens = Splitter.on(":").split(ipObj.toString());
        String host = Iterables.getFirst(tokens, null);
        String portStr = Iterables.getLast(tokens, null);
        return ImmutableList.of(new HostnamePort(host, Integer.parseInt(portStr)));
    } else if (ipObj instanceof List) {
        List<String> ips = (List) ipObj;
        List<HostnamePort> ret = new ArrayList<>();
        for (String ip : ips) {
            Iterable<String> tokens = Splitter.on(":").split(ip);
            String host = Iterables.getFirst(tokens, null);
            String portStr = Iterables.getLast(tokens, null);
            ret.add(new HostnamePort(host, Integer.parseInt(portStr)));
        }
        return ret;
    }
    throw new IllegalStateException(
            "Unable to read the elasticsearch ips, expected es.ip to be either a list of strings, a string hostname or a host:port string");
}

From source file:com.spotify.asyncdatastoreclient.Key.java

/**
 * Return element key id, or null if not set.
 *
 * This is a shortcut for {@code Key.getPath().get(Key.getPath().size() - 1).getId()}
 *
 * @return the key id./* ww w . j ava 2s .c om*/
 */
public Long getId() {
    final DatastoreV1.Key.PathElement element = Iterables.getLast(key.getPathElementList(), null);
    if (element == null) {
        return null;
    }
    return element.hasId() ? element.getId() : null;
}

From source file:cpw.mods.fml.common.LoadController.java

public ModState getModState(ModContainer selectedMod) {
    return Iterables.getLast(modStates.get(selectedMod.getModId()), ModState.AVAILABLE);
}

From source file:com.spotify.asyncdatastoreclient.Key.java

/**
 * Return element key name, or null if not set.
 *
 * This is a shortcut for {@code Key.getPath().get(Key.getPath().size() - 1).getName()}
 *
 * @return the key name./*  w w w. jav a2  s. com*/
 */
public String getName() {
    final DatastoreV1.Key.PathElement element = Iterables.getLast(key.getPathElementList(), null);
    if (element == null) {
        return null;
    }
    return element.hasName() ? element.getName() : null;
}

From source file:org.apache.metron.common.stellar.shell.StellarShell.java

/**
 * Handles user interaction when executing a Stellar expression.
 * @param expression The expression to execute.
 *///from   w ww.  j  av  a  2  s  .c om
private void handleStellar(String expression) {

    Iterable<String> assignmentSplit = Splitter.on(":=").split(expression);
    String stellarExpression = expression;
    String variable = null;
    if (Iterables.size(assignmentSplit) == 2) {
        //assignment
        variable = Iterables.getFirst(assignmentSplit, null);
        if (variable != null) {
            variable = variable.trim();
        }
        stellarExpression = Iterables.getLast(assignmentSplit, null);
    }
    if (!stellarExpression.isEmpty()) {
        stellarExpression = stellarExpression.trim();
    }
    Object result = executeStellar(stellarExpression);
    if (result != null && variable == null) {
        writeLine(result.toString());
    }
    if (variable != null) {
        executor.assign(variable, stellarExpression, result);
    }
}

From source file:org.onebusaway.nyc.vehicle_tracking.impl.inference.BlockStateSamplingStrategyImpl.java

@Override
public BlockStateObservation sampleTransitionScheduleDev(BlockStateObservation parentBlockStateObs,
        Observation obs) {/*from  www.  ja va  2  s.c o  m*/
    final BlockState parentBlockState = parentBlockStateObs.getBlockState();

    final StudentTDistribution schedDist = ScheduleLikelihood.getSchedDistForBlockState(parentBlockStateObs);
    final double newSchedDev = schedDist.sample(ParticleFactoryImpl.getLocalRng());

    final int currentTime = (int) (obs.getTime() - parentBlockState.getBlockInstance().getServiceDate()) / 1000;
    final int newSchedTime = currentTime - (int) (newSchedDev * 60.0);

    final int startSchedTime = Iterables
            .getFirst(parentBlockState.getBlockInstance().getBlock().getStopTimes(), null).getStopTime()
            .getArrivalTime();
    final int endSchedTime = Iterables
            .getLast(parentBlockState.getBlockInstance().getBlock().getStopTimes(), null).getStopTime()
            .getDepartureTime();
    BlockStateObservation schedState;
    if (newSchedTime < startSchedTime) {
        schedState = _blocksFromObservationService.getBlockStateObservationFromDist(obs,
                parentBlockState.getBlockInstance(), 0.0);
    } else if (endSchedTime < newSchedTime) {
        return null;
    } else {
        schedState = _blocksFromObservationService.getBlockStateObservationFromTime(obs,
                parentBlockState.getBlockInstance(), newSchedTime);
    }

    return orientationCheck(parentBlockStateObs, schedState, obs);
}

From source file:org.apache.cassandra.db.lifecycle.LogFile.java

LogRecord getLastRecord() {
    return Iterables.getLast(records, null);
}

From source file:org.eclipse.sirius.diagram.ui.graphical.edit.policies.RegionContainerResizableEditPolicy.java

private Option<AbstractDiagramElementContainerEditPart> getLastRegionPart() {
    Iterable<AbstractDiagramElementContainerEditPart> regionParts = getRegionParts();
    return Options.newSome(Iterables.getLast(regionParts, null));
}