Example usage for com.google.common.collect Multimap keySet

List of usage examples for com.google.common.collect Multimap keySet

Introduction

In this page you can find the example usage for com.google.common.collect Multimap keySet.

Prototype

Set<K> keySet();

Source Link

Document

Returns a view collection of all distinct keys contained in this multimap.

Usage

From source file:com.ikanow.aleph2.search_service.elasticsearch.utils.ElasticsearchHadoopUtils.java

/** 
 * @param input_config - the input settings
 * @return/*w  ww.  j  a  v a  2  s . c  om*/
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static IAnalyticsAccessContext<InputFormat> getInputFormat(final Client client,
        final AnalyticThreadJobBean.AnalyticThreadJobInputBean job_input) {
    return new IAnalyticsAccessContext<InputFormat>() {
        private LinkedHashMap<String, Object> _mutable_output = null;

        @Override
        public String describe() {
            //(return the entire thing)
            return ErrorUtils.get("service_name={0} options={1}",
                    this.getAccessService().right().value().getSimpleName(), this.getAccessConfig().get());
        }

        /* (non-Javadoc)
         * @see com.ikanow.aleph2.data_model.interfaces.data_analytics.IAnalyticsAccessContext#getAccessService()
         */
        @Override
        public Either<InputFormat, Class<InputFormat>> getAccessService() {
            return Either.right((Class<InputFormat>) (Class<?>) Aleph2EsInputFormat.class);
        }

        /* (non-Javadoc)
         * @see com.ikanow.aleph2.data_model.interfaces.data_analytics.IAnalyticsAccessContext#getAccessConfig()
         */
        @Override
        public Optional<Map<String, Object>> getAccessConfig() {
            if (null != _mutable_output) {
                return Optional.of(_mutable_output);
            }
            _mutable_output = new LinkedHashMap<>();

            // Check for input record limit:
            Optional.ofNullable(job_input.config()).map(cfg -> cfg.test_record_limit_request()).ifPresent(
                    max -> _mutable_output.put(Aleph2EsInputFormat.BE_DEBUG_MAX_SIZE, Long.toString(max)));

            final String index_resource = ElasticsearchContext.READ_PREFIX
                    + ElasticsearchIndexUtils.getBaseIndexName(BeanTemplateUtils.build(DataBucketBean.class)
                            .with(DataBucketBean::full_name, job_input.resource_name_or_id()).done().get(),
                            Optional.empty())
                    + "*";

            //TODO (ALEPH-72): support multi-buckets / buckets with non-standard indexes ... also use the tmin/tmax
            // (needs MDB to pull out - because need to get the full bucket ugh)

            // Currently need to add types: 
            //TODO (ALEPH-72): 2.2.0 you _can_ just put "indexes/" to get all types - that doesn't work for all es-hadoop code though
            final Multimap<String, String> index_type_mapping = ElasticsearchIndexUtils.getTypesForIndex(client,
                    index_resource);
            final String type_resource = index_type_mapping.values().stream().collect(Collectors.toSet())
                    .stream().collect(Collectors.joining(","));
            final String final_index = getTimedIndexes(job_input, index_type_mapping,
                    new Date())
                            .map(s -> Stream
                                    .concat(s,
                                            TimeSliceDirUtils.getUntimedDirectories(
                                                    index_type_mapping.keySet().stream()))
                                    .collect(Collectors.joining(",")))
                            .orElse(index_resource);

            _mutable_output.put("es.resource", final_index + "/" + type_resource);

            _mutable_output.put("es.read.metadata", "true");
            _mutable_output.put("es.read.metadata.field", Aleph2EsInputFormat.ALEPH2_META_FIELD);

            _mutable_output.put("es.index.read.missing.as.empty", "yes");

            _mutable_output.put("es.query",
                    Optional.ofNullable(job_input.filter()).map(f -> f.get("technology_override")).map(o -> {
                        return (o instanceof String) ? o.toString()
                                : _mapper.convertValue(o, JsonNode.class).toString();
                    }).orElse("?q=*"));
            //TODO (ALEPH-72) (incorporate tmin/tmax and also add a JSON mapping for the Aleph2 crud utils)

            // Here are the parameters that can be set:
            // es.query ... can be stringified JSON or a q=string .... eg conf.set("es.query", "?q=me*");  
            //config.set("es.resource", overallIndexNames.toString()); .. .this was in the format X,Y,Z[/type],,etc which then got copied to 
            // create a simple multi-input format .. looks like i didn't do anything if no type was set, unclear if that was an optimization
            // or if it doesn't work... (if it doesn't work then what you have to do is get the mappings for each index and
            // get the types and insert them all)
            //config.set("es.index.read.missing.as.empty", "yes");

            // (not sure if need to set just normal http port/host?)
            //config.set("es.net.proxy.http.host", "localhost");
            //config.set("es.net.proxy.http.port", "8888");

            return Optional.of(Collections.unmodifiableMap(_mutable_output));
        }
    };
}

From source file:com.eucalyptus.cloudwatch.common.internal.domain.listmetrics.ListMetricManager.java

public static void addMetricBatch(List<ListMetric> dataBatch) {
    // sort the collection by common items to require fewer lookups
    Multimap<PrefetchFields, ListMetric> dataBatchPrefetchMap = LinkedListMultimap.create();
    for (final ListMetric item : dataBatch) {
        PrefetchFields prefetchFields = new PrefetchFields(item.getAccountId(), item.getNamespace(),
                item.getMetricName());/*from   w w  w.  j  a  v  a  2  s  . com*/
        dataBatchPrefetchMap.put(prefetchFields, item);
    }
    // do db stuff in a certain number of operations per connection
    for (List<PrefetchFields> prefetchFieldsListPartial : Iterables.partition(dataBatchPrefetchMap.keySet(),
            LIST_METRIC_NUM_DB_OPERATIONS_PER_TRANSACTION)) {
        try (final TransactionResource db = Entities.transactionFor(ListMetric.class)) {
            int numOperations = 0;
            for (PrefetchFields prefetchFields : prefetchFieldsListPartial) {
                // Prefetch all list metrics with same metric name/namespace/account id
                Map<NonPrefetchFields, ListMetric> dataCache = Maps.newHashMap();
                Criteria criteria = Entities.createCriteria(ListMetric.class)
                        .add(Restrictions.eq("accountId", prefetchFields.getAccountId()))
                        .add(Restrictions.eq("namespace", prefetchFields.getNamespace()))
                        .add(Restrictions.eq("metricName", prefetchFields.getMetricName()));
                List<ListMetric> results = (List<ListMetric>) criteria.list();
                for (ListMetric result : results) {
                    dataCache.put(new NonPrefetchFields(result.getMetricType(), result.getDimensionMap()),
                            result);
                }
                for (ListMetric listMetric : dataBatchPrefetchMap.get(prefetchFields)) {
                    NonPrefetchFields cacheKey = new NonPrefetchFields(listMetric.getMetricType(),
                            listMetric.getDimensionMap());
                    if (dataCache.containsKey(cacheKey)) {
                        dataCache.get(cacheKey).updateTimeStamps();
                    } else {
                        Entities.persist(listMetric);
                        dataCache.put(cacheKey, listMetric);
                    }
                }
                numOperations++;
                if (numOperations % LIST_METRIC_NUM_DB_OPERATIONS_UNTIL_SESSION_FLUSH == 0) {
                    Entities.flushSession(ListMetric.class);
                    Entities.clearSession(ListMetric.class);
                }
            }
            db.commit();
        }
    }
}

From source file:org.crypto.sse.DynRH.java

public static TreeMultimap<String, byte[]> tokenUpdate(byte[] key, Multimap<String, String> lookup)
        throws InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException,
        NoSuchProviderException, NoSuchPaddingException, IOException {

    // We use a lexicographic sorted list such that the server
    // will not know any information of the inserted elements creation order
    TreeMultimap<String, byte[]> tokenUp = TreeMultimap.create(Ordering.natural(), Ordering.usingToString());
    // Key generation

    SecureRandom random = new SecureRandom();
    random.setSeed(CryptoPrimitives.randomSeed(16));
    byte[] iv = new byte[16];

    for (String word : lookup.keySet()) {

        byte[] key1 = CryptoPrimitives.generateCmac(key, 1 + new String());

        byte[] key2 = CryptoPrimitives.generateCmac(key, 2 + word);

        for (String id : lookup.get(word)) {

            random.nextBytes(iv);/*from  w  ww  .  ja  v  a2  s.  c o  m*/

            int counter = 0;

            if (state.get(word) != null) {
                counter = state.get(word);
            }

            state.put(word, counter + 1);

            byte[] l = CryptoPrimitives.generateCmac(key2, "" + counter);

            byte[] value = CryptoPrimitives.encryptAES_CTR_String(key1, iv, id, sizeOfFileIdentifer);
            tokenUp.put(new String(l), value);
        }

    }
    return tokenUp;
}

From source file:com.bigdata.dastor.service.StorageProxy.java

private static void assureSufficientLiveNodes(int blockFor, Collection<InetAddress> writeEndpoints,
        Multimap<InetAddress, InetAddress> hintedEndpoints, ConsistencyLevel consistencyLevel)
        throws UnavailableException {
    if (consistencyLevel == ConsistencyLevel.ANY) {
        // ensure there are blockFor distinct living nodes (hints are ok).
        if (hintedEndpoints.keySet().size() < blockFor)
            throw new UnavailableException();
    }/*from  ww w  .  jav a 2s. c om*/

    // count destinations that are part of the desired target set
    int liveNodes = 0;
    for (InetAddress destination : hintedEndpoints.keySet()) {
        if (writeEndpoints.contains(destination))
            liveNodes++;
    }
    if (liveNodes < blockFor) {
        throw new UnavailableException();
    }
}

From source file:org.opentripplanner.routing.edgetype.TripPattern.java

/**
 * Static method that creates unique human-readable names for a collection of TableTripPatterns.
 * Perhaps this should be in TripPattern, and apply to Frequency patterns as well. TODO: resolve
 * this question: can a frequency and table pattern have the same stoppattern? If so should they
 * have the same "unique" name?/*w w  w .j av a  2 s.  co  m*/
 * 
 * The names should be dataset unique, not just route-unique?
 * 
 * A TripPattern groups all trips visiting a particular pattern of stops on a particular route.
 * GFTS Route names are intended for very general customer information, but sometimes there is a
 * need to know where a particular trip actually goes. For example, the New York City N train
 * has at least four different variants: express (over the Manhattan bridge) and local (via
 * lower Manhattan and the tunnel), in two directions (to Astoria or to Coney Island). During
 * construction, a fifth variant sometimes appears: trains use the D line to Coney Island after
 * 59th St (or from Coney Island to 59th in the opposite direction).
 * 
 * TripPattern names are machine-generated on a best-effort basis. They are guaranteed to be
 * unique (among TripPatterns for a single Route) but not stable across graph builds, especially
 * when different versions of GTFS inputs are used. For instance, if a variant is the only
 * variant of the N that ends at Coney Island, the name will be "N to Coney Island". But if
 * multiple variants end at Coney Island (but have different stops elsewhere), that name would
 * not be chosen. OTP also tries start and intermediate stations ("from Coney Island", or "via
 * Whitehall", or even combinations ("from Coney Island via Whitehall"). But if there is no way
 * to create a unique name from start/end/intermediate stops, then the best we can do is to
 * create a "like [trip id]" name, which at least tells you where in the GTFS you can find a
 * related trip.
 */
// TODO: pass in a transit index that contains a Multimap<Route, TripPattern> and derive all TableTripPatterns
// TODO: use headsigns before attempting to machine-generate names
// TODO: combine from/to and via in a single name. this could be accomplished by grouping the trips by destination,
// then disambiguating in groups of size greater than 1.
/*
 * Another possible approach: for each route, determine the necessity of each field (which
 * combination will create unique names). from, to, via, express. Then concatenate all necessary
 * fields. Express should really be determined from number of stops and/or run time of trips.
 */
public static void generateUniqueNames(Collection<TripPattern> tableTripPatterns) {
    LOG.info("Generating unique names for stop patterns on each route.");
    Set<String> usedRouteNames = Sets.newHashSet();
    Map<Route, String> uniqueRouteNames = Maps.newHashMap();

    /* Group TripPatterns by Route */
    Multimap<Route, TripPattern> patternsByRoute = ArrayListMultimap.create();
    for (TripPattern ttp : tableTripPatterns) {
        patternsByRoute.put(ttp.route, ttp);
    }

    /* Ensure we have a unique name for every Route */
    for (Route route : patternsByRoute.keySet()) {
        String routeName = GtfsLibrary.getRouteName(route);
        if (usedRouteNames.contains(routeName)) {
            int i = 2;
            String generatedRouteName;
            do
                generatedRouteName = routeName + " " + (i++);
            while (usedRouteNames.contains(generatedRouteName));
            LOG.warn("Route had non-unique name. Generated one to ensure uniqueness of TripPattern names: {}",
                    generatedRouteName);
            routeName = generatedRouteName;
        }
        usedRouteNames.add(routeName);
        uniqueRouteNames.put(route, routeName);
    }

    /* Iterate over all routes, giving the patterns within each route unique names. */
    ROUTE: for (Route route : patternsByRoute.keySet()) {
        Collection<TripPattern> routeTripPatterns = patternsByRoute.get(route);
        String routeName = uniqueRouteNames.get(route);

        /* Simplest case: there's only one route variant, so we'll just give it the route's name. */
        if (routeTripPatterns.size() == 1) {
            routeTripPatterns.iterator().next().name = routeName;
            continue;
        }

        /* Do the patterns within this Route have a unique start, end, or via Stop? */
        Multimap<String, TripPattern> signs = ArrayListMultimap.create(); // prefer headsigns
        Multimap<Stop, TripPattern> starts = ArrayListMultimap.create();
        Multimap<Stop, TripPattern> ends = ArrayListMultimap.create();
        Multimap<Stop, TripPattern> vias = ArrayListMultimap.create();
        for (TripPattern pattern : routeTripPatterns) {
            List<Stop> stops = pattern.getStops();
            Stop start = stops.get(0);
            Stop end = stops.get(stops.size() - 1);
            starts.put(start, pattern);
            ends.put(end, pattern);
            for (Stop stop : stops)
                vias.put(stop, pattern);
        }
        PATTERN: for (TripPattern pattern : routeTripPatterns) {
            List<Stop> stops = pattern.getStops();
            StringBuilder sb = new StringBuilder(routeName);

            /* First try to name with destination. */
            Stop end = stops.get(stops.size() - 1);
            sb.append(" to " + stopNameAndId(end));
            if (ends.get(end).size() == 1) {
                pattern.name = sb.toString();
                continue PATTERN; // only pattern with this last stop
            }

            /* Then try to name with origin. */
            Stop start = stops.get(0);
            sb.append(" from " + stopNameAndId(start));
            if (starts.get(start).size() == 1) {
                pattern.name = (sb.toString());
                continue PATTERN; // only pattern with this first stop
            }

            /* Check whether (end, start) is unique. */
            Set<TripPattern> remainingPatterns = Sets.newHashSet();
            remainingPatterns.addAll(starts.get(start));
            remainingPatterns.retainAll(ends.get(end)); // set intersection
            if (remainingPatterns.size() == 1) {
                pattern.name = (sb.toString());
                continue PATTERN;
            }

            /* Still not unique; try (end, start, via) for each via. */
            for (Stop via : stops) {
                if (via.equals(start) || via.equals(end))
                    continue;
                Set<TripPattern> intersection = Sets.newHashSet();
                intersection.addAll(remainingPatterns);
                intersection.retainAll(vias.get(via));
                if (intersection.size() == 1) {
                    sb.append(" via " + stopNameAndId(via));
                    pattern.name = (sb.toString());
                    continue PATTERN;
                }
            }

            /* Still not unique; check for express. */
            if (remainingPatterns.size() == 2) {
                // There are exactly two patterns sharing this start/end.
                // The current one must be a subset of the other, because it has no unique via. 
                // Therefore we call it the express.
                sb.append(" express");
            } else {
                // The final fallback: reference a specific trip ID.
                sb.append(" like trip " + pattern.getTrips().get(0).getId());
            }
            pattern.name = (sb.toString());
        } // END foreach PATTERN
    } // END foreach ROUTE

    if (LOG.isDebugEnabled()) {
        LOG.debug("Done generating unique names for stop patterns on each route.");
        for (Route route : patternsByRoute.keySet()) {
            Collection<TripPattern> routeTripPatterns = patternsByRoute.get(route);
            LOG.debug("Named {} patterns in route {}", routeTripPatterns.size(), uniqueRouteNames.get(route));
            for (TripPattern pattern : routeTripPatterns) {
                LOG.debug("    {} ({} stops)", pattern.name, pattern.stopPattern.size);
            }
        }
    }

}

From source file:moa2014.MOAH52014.java

public static int principal(int[][] matrizatual) {
    long startTime = System.currentTimeMillis();
    Multimap<Integer, String> open_list = TreeMultimap.create();
    HashMap<String, Estado> processados = new HashMap();

    int h1x = MOAH12014.diferencaMatriz(matrizatual);
    int h2x = MOAH22014.diferencaMatriz(matrizatual);
    int h3x = MOAH32014.diferencaMatriz(matrizatual);

    int difmatrizatual = maior(h1x, h2x, h3x);

    String stringmatriz = transformaMatrizString(matrizatual);
    open_list.put(difmatrizatual, stringmatriz);
    Estado estadoatual = new Estado(matrizatual, 0);
    processados.put(stringmatriz, estadoatual);

    int arvoresgeradas = 0;
    int arvoresprocessadas = 0;

    while (!open_list.isEmpty()) {

        Iterator iterator = open_list.keySet().iterator();

        Integer key = (Integer) iterator.next();
        String matrizatualx1 = open_list.asMap().get(key).iterator().next();
        Estado estadomenor = processados.get(matrizatualx1);
        int altura = estadomenor.getCusto();

        //LOCALIZA O ZERO
        int[] zerot = localizazero(estadomenor.getMatriz());
        int x = zerot[0];
        int y = zerot[1];
        int x0 = x - 1;
        int x1 = x + 1;
        int y0 = y - 1;
        int y1 = y + 1;
        int difmatrizatualx = MOAH32014.diferencaMatriz(estadomenor.getMatriz());
        if (difmatrizatualx == 0) {
            long endTime = System.currentTimeMillis();
            System.out.println("---------------------------------------");
            System.out.println("Arvores Geradas: " + arvoresgeradas);
            System.out.println("Arvores Processadas: " + arvoresprocessadas);
            System.out.println("Quantidade de Movimentos: " + estadomenor.getCusto());
            System.out.println("Tempo de processamento " + (endTime - startTime) + " ms");
            System.out.println("---------------------------------------\n\n");
            return 0;
        }/*from w  w w. j  av a  2  s. c o  m*/
        int[][] matrizatualx = estadomenor.getMatriz();
        arvoresprocessadas++;
        if (x0 >= 0) {

            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x0][y];
            matriz[x0][y] = matrizatualx[x][y];

            String stringmatriz1 = transformaMatrizString(matriz);
            if (!(processados.containsKey(stringmatriz1))) {
                arvoresgeradas++;
                h1x = MOAH12014.diferencaMatriz(matriz);
                h2x = MOAH22014.diferencaMatriz(matriz);
                h3x = MOAH32014.diferencaMatriz(matriz);
                int diferencamatriz = maior(h1x, h2x, h3x);
                int custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz1);

                processados.put(stringmatriz1, estadonovo);

            }
        }
        if (x1 <= 3) {
            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x1][y];
            matriz[x1][y] = matrizatualx[x][y];
            String stringmatriz2 = transformaMatrizString(matriz);

            if (!(processados.containsKey(stringmatriz2))) {
                arvoresgeradas++;
                h1x = MOAH12014.diferencaMatriz(matriz);
                h2x = MOAH22014.diferencaMatriz(matriz);
                h3x = MOAH32014.diferencaMatriz(matriz);
                int diferencamatriz = maior(h1x, h2x, h3x);
                int custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz2);

                processados.put(stringmatriz2, estadonovo);

            }
        }
        if (y0 >= 0) {
            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x][y0];
            matriz[x][y0] = matrizatualx[x][y];
            String stringmatriz3 = transformaMatrizString(matriz);

            if (!(processados.containsKey(stringmatriz3))) {
                arvoresgeradas++;
                h1x = MOAH12014.diferencaMatriz(matriz);
                h2x = MOAH22014.diferencaMatriz(matriz);
                h3x = MOAH32014.diferencaMatriz(matriz);
                int diferencamatriz = maior(h1x, h2x, h3x);
                int custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz3);

                processados.put(stringmatriz3, estadonovo);

            }
        }
        if (y1 <= 3) {
            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x][y1];
            matriz[x][y1] = matrizatualx[x][y];

            String stringmatriz4 = transformaMatrizString(matriz);

            if (!(processados.containsKey(stringmatriz4))) {
                arvoresgeradas++;
                h1x = MOAH12014.diferencaMatriz(matriz);
                h2x = MOAH22014.diferencaMatriz(matriz);
                h3x = MOAH32014.diferencaMatriz(matriz);
                int diferencamatriz = maior(h1x, h2x, h3x);
                int custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz4);

                processados.put(stringmatriz4, estadonovo);

            }
        }
        open_list.remove(key, matrizatualx1);
    }
    return 0;

}

From source file:moa2014.MOAH42014.java

public static int principal(int[][] matrizatual, double c1, double c2, double c3) {
    long startTime = System.currentTimeMillis();
    Multimap<Double, String> open_list = TreeMultimap.create();
    HashMap<String, Estado> processados = new HashMap();

    double h1 = c1 * h1(matrizatual);
    double h2 = c2 * h2(matrizatual);
    double h3 = c3 * h3(matrizatual);
    double difmatrizatual = h1 + h2 + h3;

    String stringmatriz = transformaMatrizString(matrizatual);
    open_list.put(difmatrizatual, stringmatriz);
    Estado estadoatual = new Estado(matrizatual, 0);
    processados.put(stringmatriz, estadoatual);

    int arvoresgeradas = 0;
    int arvoresprocessadas = 0;

    while (!open_list.isEmpty()) {

        Iterator iterator = open_list.keySet().iterator();

        Double key = (Double) iterator.next();
        String matrizatualx1 = open_list.asMap().get(key).iterator().next();
        Estado estadomenor = processados.get(matrizatualx1);
        int altura = estadomenor.getCusto();

        //LOCALIZA O ZERO
        int[] zerot = localizazero(estadomenor.getMatriz());
        int x = zerot[0];
        int y = zerot[1];
        int x0 = x - 1;
        int x1 = x + 1;
        int y0 = y - 1;
        int y1 = y + 1;
        int difmatrizatualx = h1(estadomenor.getMatriz());
        if (difmatrizatualx == 0) {
            long endTime = System.currentTimeMillis();
            System.out.println("---------------------------------------");
            System.out.println("Arvores Geradas: " + arvoresgeradas);
            System.out.println("Arvores Processadas: " + arvoresprocessadas);
            System.out.println("Quantidade de Movimentos: " + estadomenor.getCusto());
            System.out.println("Tempo de processamento " + (endTime - startTime) + " ms");
            System.out.println("---------------------------------------\n\n");
            return 0;
        }/* w w  w .  j  a v  a2  s.c o m*/
        arvoresprocessadas++;
        int[][] matrizatualx = estadomenor.getMatriz();
        if (x0 >= 0) {

            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x0][y];
            matriz[x0][y] = matrizatualx[x][y];

            String stringmatriz1 = transformaMatrizString(matriz);
            if (!(processados.containsKey(stringmatriz1))) {
                arvoresgeradas++;
                h1 = c1 * h1(matriz);
                h2 = c2 * h2(matriz);
                h3 = c3 * h3(matriz);
                double diferencamatriz = h1 + h2 + h3;
                double custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz1);

                processados.put(stringmatriz1, estadonovo);

            }
        }
        if (x1 <= 3) {
            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x1][y];
            matriz[x1][y] = matrizatualx[x][y];
            String stringmatriz2 = transformaMatrizString(matriz);

            if (!(processados.containsKey(stringmatriz2))) {
                arvoresgeradas++;
                h1 = c1 * h1(matriz);
                h2 = c2 * h2(matriz);
                h3 = c3 * h3(matriz);
                double diferencamatriz = h1 + h2 + h3;
                double custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz2);

                processados.put(stringmatriz2, estadonovo);

            }
        }
        if (y0 >= 0) {
            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x][y0];
            matriz[x][y0] = matrizatualx[x][y];
            String stringmatriz3 = transformaMatrizString(matriz);

            if (!(processados.containsKey(stringmatriz3))) {
                arvoresgeradas++;
                h1 = c1 * h1(matriz);
                h2 = c2 * h2(matriz);
                h3 = c3 * h3(matriz);
                double diferencamatriz = h1 + h2 + h3;
                double custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz3);

                processados.put(stringmatriz3, estadonovo);

            }
        }
        if (y1 <= 3) {
            int[][] matriz;
            matriz = copyarray(matrizatualx);
            matriz[x][y] = matrizatualx[x][y1];
            matriz[x][y1] = matrizatualx[x][y];

            String stringmatriz4 = transformaMatrizString(matriz);

            if (!(processados.containsKey(stringmatriz4))) {
                arvoresgeradas++;
                h1 = c1 * h1(matriz);
                h2 = c2 * h2(matriz);
                h3 = c3 * h3(matriz);
                double diferencamatriz = h1 + h2 + h3;
                double custototal = diferencamatriz + altura + 1;

                Estado estadonovo = new Estado(matriz, altura + 1);
                open_list.put(custototal, stringmatriz4);

                processados.put(stringmatriz4, estadonovo);

            }
        }
        open_list.remove(key, matrizatualx1);
    }
    return 0;

}

From source file:org.crypto.sse.RH2Lev.java

public static RH2Lev constructEMMPar(final byte[] key, final Multimap<String, String> lookup,
        final int bigBlock, final int smallBlock, final int dataSize)
        throws InterruptedException, ExecutionException, IOException {

    final Multimap<String, byte[]> dictionary = ArrayListMultimap.create();

    for (int i = 0; i < dataSize; i++) {
        free.add(i);/*from w ww.  java  2  s  .co  m*/
    }
    random.setSeed(CryptoPrimitives.randomSeed(16));

    List<String> listOfKeyword = new ArrayList<String>(lookup.keySet());
    int threads = 0;
    if (Runtime.getRuntime().availableProcessors() > listOfKeyword.size()) {
        threads = listOfKeyword.size();
    } else {
        threads = Runtime.getRuntime().availableProcessors();
    }

    ExecutorService service = Executors.newFixedThreadPool(threads);
    ArrayList<String[]> inputs = new ArrayList<String[]>(threads);

    final Map<Integer, String> concurrentMap = new ConcurrentHashMap<Integer, String>();
    for (int i = 0; i < listOfKeyword.size(); i++) {
        concurrentMap.put(i, listOfKeyword.get(i));
    }

    for (int j = 0; j < threads; j++) {
        service.execute(new Runnable() {
            @SuppressWarnings("unused")
            @Override
            public void run() {

                while (concurrentMap.keySet().size() > 0) {

                    Random rand = new Random();
                    String[] input = { "" };
                    synchronized (concurrentMap) {
                        Set<Integer> possibleValues = concurrentMap.keySet();
                        int temp = rand.nextInt(possibleValues.size());
                        List<Integer> listOfPossibleKeywords = new ArrayList<Integer>(possibleValues);
                        // set the input as randomly selected from the remaining
                        // possible keys
                        input[0] = concurrentMap.get(listOfPossibleKeywords.get(temp));
                        // remove the key
                        concurrentMap.remove(listOfPossibleKeywords.get(temp));
                    }

                    try {

                        Multimap<String, byte[]> output = setup(key, input, lookup, bigBlock, smallBlock,
                                dataSize);
                        Set<String> keys = output.keySet();

                        for (String k : keys) {
                            dictionary.putAll(k, output.get(k));
                        }
                    } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchProviderException
                            | NoSuchPaddingException | IOException | InvalidAlgorithmParameterException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    service.shutdown();

    // Blocks until all tasks have completed execution after a shutdown
    // request
    service.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);

    return new RH2Lev(dictionary, array);
}

From source file:org.pau.assetmanager.viewmodel.chart.ChartDataModel.java

/**
 * Returns the total balance along one year in a monthly basis.
 * // w  w  w .j a  va2  s. c o m
 * @param year
 *            the balance
 * @param yearToAnnotationMultimap
 *            year --> annotations multimap
 * @param selectedBook
 *            book selected for the balance
 * @param bookSelectionType
 *            type of book
 * @return
 */
public static DefaultCategoryDataset getBalance(Integer year,
        Multimap<Integer, Annotation> yearToAnnotationMultimap, BookSelection bookSelection,
        Boolean includePurchasedStocks, Optional<String> optionalStockLabel) {
    Book selectedBook = bookSelection.getSelectedBook();
    Double previousYearsTotals = 0.0;
    for (Integer currentYear : yearToAnnotationMultimap.keySet()) {
        if (currentYear < year) {
            for (Annotation currentAnnotation : yearToAnnotationMultimap.get(currentYear)) {
                previousYearsTotals += currentAnnotation.getSignedAmount();
            }
        }
    }
    Set<StocksBook> stockBooks = new HashSet<StocksBook>();
    Map<String, Double> totalData = new HashMap<String, Double>();
    for (int month = 0; month < 12; month++) {
        Calendar currentcalendar = GregorianCalendar.getInstance();
        currentcalendar.set(Calendar.MONTH, month);
        currentcalendar.set(Calendar.YEAR, year);
        currentcalendar.set(Calendar.DAY_OF_MONTH, 1);
        String mainLabel = getTimeLabel(currentcalendar.getTime());
        totalData.put(mainLabel, 0.0);
        Collection<Annotation> annotations = yearToAnnotationMultimap.get(year);
        if (annotations != null) {
            for (Annotation annotation : annotations) {
                String currentAnnotationLabel = getTimeLabel(annotation.getDate());
                if (currentAnnotationLabel.equals(mainLabel)) {
                    if (annotation.getBook() instanceof StocksBook) {
                        stockBooks.add((StocksBook) annotation.getBook());
                    }
                    totalData.put(mainLabel, totalData.get(mainLabel) + annotation.getSignedAmount());
                }
            }
        }
    }

    DefaultCategoryDataset model = new DefaultCategoryDataset();
    List<String> listOfLabels = getListOfMonthTimeLabelsForYear(year);

    Map<String, Double> labelToToalCumulativeMap = new HashMap<String, Double>();

    Double total = 0.0;
    for (String currentLabel : listOfLabels) {
        Double currentValue = totalData.get(currentLabel);
        if (currentValue != null) {
            total += currentValue;
        }
        labelToToalCumulativeMap.put(currentLabel, total + previousYearsTotals);
    }

    for (String currentLabel : listOfLabels) {
        model.addValue(labelToToalCumulativeMap.get(currentLabel), TOTAL_CUMULATIVE, currentLabel);
    }

    // add stock information in case it is necessary
    if ((selectedBook instanceof StocksBook) && includePurchasedStocks) {
        Table<Integer, Integer, Set<StockState>> historicalStockBalanceStateTable = HistoricalStockBalanceState
                .getConiniousHistoricalStockBalanceStateTable(bookSelection, year, optionalStockLabel);
        Collection<Annotation> annotationsForYear = yearToAnnotationMultimap.get(year);
        if (annotationsForYear == null) {
            annotationsForYear = Lists.newLinkedList();
        }
        Collection<Annotation> movementAnnotationsForYear = Collections2.filter(annotationsForYear,
                new Predicate<Annotation>() {
                    @Override
                    public boolean apply(Annotation input) {
                        return input instanceof MovementExpensesAnnotation
                                || input instanceof MovementIncomeAnnotation;
                    }
                });
        movementAnnotationsForYear = Collections2.filter(movementAnnotationsForYear,
                new Predicate<Annotation>() {
                    @Override
                    public boolean apply(Annotation input) {
                        return input instanceof MovementExpensesAnnotation
                                || input instanceof MovementIncomeAnnotation;
                    }
                });
        final Calendar currentcalendar = GregorianCalendar.getInstance();
        Multimap<Integer, Annotation> monthToAnnotationMultimap = Multimaps.index(movementAnnotationsForYear,
                new Function<Annotation, Integer>() {
                    @Override
                    public Integer apply(Annotation input) {
                        currentcalendar.setTime(input.getDate());
                        return currentcalendar.get(Calendar.MONTH);
                    }
                });

        Double movementBalance = getAllPreviousMovementCumulative(yearToAnnotationMultimap, year);
        if (historicalStockBalanceStateTable != null) {
            Map<Integer, Set<StockState>> historyForYear = historicalStockBalanceStateTable.rowMap().get(year);
            for (Integer currentMonth = 0; currentMonth <= 11; currentMonth++) {
                Double balance = 0.0;
                if (historyForYear != null && historyForYear.get(currentMonth) != null) {
                    for (StockState currentStockState : historyForYear.get(currentMonth)) {
                        balance += currentStockState.getVirtualBalanceForForcedYearAndMonth(year, currentMonth);
                    }
                } else {
                    balance = getYearBeforeFinalVirtualStockBalance(year, historicalStockBalanceStateTable);
                }
                if (monthToAnnotationMultimap.get(currentMonth) != null) {
                    for (Annotation movementAnnotation : monthToAnnotationMultimap.get(currentMonth)) {
                        movementBalance += movementAnnotation.getSignedAmount();
                    }
                }
                model.addValue(balance, TOTAL_CUMULATIVE_STOCKS_PURCHASED, listOfLabels.get(currentMonth));
                Double cumulativeBalance = labelToToalCumulativeMap.get(listOfLabels.get(currentMonth));

                model.addValue(balance + cumulativeBalance - movementBalance, TOTAL_CUMULATIVE_STOCKS_COMBINED,
                        listOfLabels.get(currentMonth));
            }

        }
    }

    return model;
}

From source file:org.crypto.sse.EMM2Lev.java

public static RH2Lev constructEMMPar(final byte[] key, final Multimap<String, String> lookup,
        final int bigBlock, final int smallBlock, final int dataSize)
        throws InterruptedException, ExecutionException, IOException {

    final Multimap<String, byte[]> dictionary = ArrayListMultimap.create();

    for (int i = 0; i < dataSize; i++) {
        free.add(i);//ww w .j a v  a 2  s.com
    }
    random.setSeed(CryptoPrimitives.randomSeed(16));

    List<String> listOfKeyword = new ArrayList<String>(lookup.keySet());
    int threads = 0;
    if (Runtime.getRuntime().availableProcessors() > listOfKeyword.size()) {
        threads = listOfKeyword.size();
    } else {
        threads = Runtime.getRuntime().availableProcessors();
    }

    ExecutorService service = Executors.newFixedThreadPool(threads);
    ArrayList<String[]> inputs = new ArrayList<String[]>(threads);

    final Map<Integer, String> concurrentMap = new ConcurrentHashMap<Integer, String>();
    for (int i = 0; i < listOfKeyword.size(); i++) {
        concurrentMap.put(i, listOfKeyword.get(i));
    }

    for (int j = 0; j < threads; j++) {
        service.execute(new Runnable() {
            @SuppressWarnings("unused")
            @Override
            public void run() {

                while (concurrentMap.keySet().size() > 0) {
                    Set<Integer> possibleValues = concurrentMap.keySet();

                    Random rand = new Random();

                    int temp = rand.nextInt(possibleValues.size());

                    List<Integer> listOfPossibleKeywords = new ArrayList<Integer>(possibleValues);

                    // set the input as randomly selected from the remaining
                    // possible keys
                    String[] input = { concurrentMap.get(listOfPossibleKeywords.get(temp)) };

                    // remove the key
                    concurrentMap.remove(listOfPossibleKeywords.get(temp));

                    try {

                        Multimap<String, byte[]> output = setup(key, input, lookup, bigBlock, smallBlock,
                                dataSize);
                        Set<String> keys = output.keySet();

                        for (String k : keys) {
                            dictionary.putAll(k, output.get(k));
                        }
                    } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchProviderException
                            | NoSuchPaddingException | IOException | InvalidAlgorithmParameterException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    service.shutdown();

    // Blocks until all tasks have completed execution after a shutdown
    // request
    service.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);

    return new RH2Lev(dictionary, array);
}