List of usage examples for java.util ArrayList set
public E set(int index, E element)
From source file:com.spoiledmilk.cykelsuperstier.break_rote.LocalTrainData.java
private static void getTimesForWeekend(int hour, int minute, ArrayList<ITransportationInfo> times, JsonNode timeTableNode, int index1, int index2, int numOfStations, JsonNode dataNodeArray) { ArrayList<TimeData> allTimes = new ArrayList<TimeData>(); for (int i = 0; i < timeTableNode.size(); i++) { JsonNode intervalNode = timeTableNode.get(i); try {//ww w . java2 s .c o m double currentTime = ((double) hour) + ((double) minute) / 100d; double startInterval = intervalNode.get("start-time").asDouble(); double endInterval = intervalNode.get("end-time").asDouble(); if (endInterval < 1.0) endInterval += 24.0; if (currentTime >= startInterval && currentTime <= endInterval) { JsonNode dataNode = dataNodeArray.get(i); int[] minutesArray = new int[dataNode.size()]; for (int j = 0; j < minutesArray.length; j++) { minutesArray[j] = dataNode.get(j).asInt(); } int currentHour = hour; int currentIndex = 0; int count = 0; while (currentHour <= endInterval && count < 3 && currentIndex < minutesArray.length) { double time1 = minutesArray[currentIndex + index1]; double time2 = minutesArray[currentIndex + index2]; if (time1 < 0 || time2 < 0) continue; time1 /= 100d; time1 += currentHour; time2 /= 100d; time2 += currentHour; if (time2 < time1) time2 += 1.0; allTimes.add(new TimeData(time1, time2)); currentHour++; count++; currentIndex += numOfStations; } } } catch (Exception e) { LOG.e(e.getLocalizedMessage()); } } // sort the list for (int i = 0; i < allTimes.size() - 1; i++) { for (int j = i + 1; j < allTimes.size(); j++) { TimeData td1 = allTimes.get(i); TimeData td2 = allTimes.get(j); if (td1.stationAtime > td2.stationAtime) { allTimes.set(i, td2); allTimes.set(j, td1); } } } LOG.d("sorted times = " + allTimes.toString()); // return the 3 results for (int i = 0; i < 3 && i < allTimes.size(); i++) { TimeData temp = allTimes.get(i); int time = 0; int minutes1 = (int) ((temp.stationAtime - (double) ((int) temp.stationAtime)) * 100); int minutes2 = (int) ((temp.stationBtime - (double) ((int) temp.stationBtime)) * 100); time = (((int) temp.stationBtime) - ((int) temp.stationAtime)) * 60; if (minutes2 >= minutes1) time = minutes2 - minutes1; else { time += 60 - minutes1 + minutes2; time -= 60; } String formattedTime1 = (((int) temp.stationAtime) < 10 ? "0" + ((int) temp.stationAtime) : "" + ((int) temp.stationAtime)) + ":" + (minutes1 < 10 ? "0" + minutes1 : "" + minutes1); String formattedTime2 = (((int) temp.stationBtime) < 10 ? "0" + ((int) temp.stationBtime) : "" + ((int) temp.stationBtime)) + ":" + (minutes2 < 10 ? "0" + minutes2 : "" + minutes2); times.add(new LocalTrainData(formattedTime1, formattedTime2, time)); } }
From source file:com.spoiledmilk.cykelsuperstier.break_rote.LocalTrainData.java
private static void getTimesForWeekdays(int hour, int minute, ArrayList<ITransportationInfo> times, JsonNode timeTableNode, int index1, int index2, int numOfStations) { ArrayList<TimeData> allTimes = new ArrayList<TimeData>(); for (int i = 0; i < timeTableNode.size(); i++) { JsonNode intervalNode = timeTableNode.get(i); try {//from ww w. j av a 2 s.c om double currentTime = ((double) hour) + ((double) minute) / 100d; double startInterval = intervalNode.get("start-time").asDouble(); double endInterval = intervalNode.get("end-time").asDouble(); if (endInterval < 1.0) endInterval += 24.0; if (currentTime >= startInterval && currentTime <= endInterval) { JsonNode dataNode = intervalNode.get("data"); int[] minutesArray = new int[dataNode.size()]; for (int j = 0; j < minutesArray.length; j++) { minutesArray[j] = dataNode.get(j).asInt(); } int currentHour = hour; int currentIndex = 0; int count = 0; while (currentHour <= endInterval && count < 3 && currentIndex < minutesArray.length) { double time1 = minutesArray[currentIndex + index1]; double time2 = minutesArray[currentIndex + index2]; if (time1 < 0 || time2 < 0) continue; time1 /= 100d; time1 += currentHour; time2 /= 100d; time2 += currentHour; if (time2 < time1) time2 += 1.0; allTimes.add(new TimeData(time1, time2)); currentHour++; count++; currentIndex += numOfStations; } } } catch (Exception e) { LOG.e(e.getLocalizedMessage()); } } // sort the list for (int i = 0; i < allTimes.size() - 1; i++) { for (int j = i + 1; j < allTimes.size(); j++) { TimeData td1 = allTimes.get(i); TimeData td2 = allTimes.get(j); if (td1.stationAtime > td2.stationAtime) { allTimes.set(i, td2); allTimes.set(j, td1); } } } LOG.d("sorted times = " + allTimes.toString()); // return the 3 results for (int i = 0; i < 3 && i < allTimes.size(); i++) { TimeData temp = allTimes.get(i); int time = 0; int minutes1 = (int) ((temp.stationAtime - (double) ((int) temp.stationAtime)) * 100); int minutes2 = (int) ((temp.stationBtime - (double) ((int) temp.stationBtime)) * 100); time = (((int) temp.stationBtime) - ((int) temp.stationAtime)) * 60; if (minutes2 >= minutes1) time = minutes2 - minutes1; else { time += 60 - minutes1 + minutes2; time -= 60; } String formattedTime1 = (((int) temp.stationAtime) < 10 ? "0" + ((int) temp.stationAtime) : "" + ((int) temp.stationAtime)) + ":" + (minutes1 < 10 ? "0" + minutes1 : "" + minutes1); String formattedTime2 = (((int) temp.stationBtime) < 10 ? "0" + ((int) temp.stationBtime) : "" + ((int) temp.stationBtime)) + ":" + (minutes2 < 10 ? "0" + minutes2 : "" + minutes2); times.add(new LocalTrainData(formattedTime1, formattedTime2, time)); } }
From source file:com.github.jackygurui.vertxredissonrepository.repository.Impl.RedisRepositoryImpl.java
private void getByListBlocking(List<String> ids, AsyncResultHandler<List<T>> resultHandler) { if (ids == null) { resultHandler.handle(Future.failedFuture(new IllegalArgumentException("List of ids can't be null."))); return;//from ww w . j av a 2 s. c o m } else if (ids.isEmpty()) { resultHandler.handle(Future.succeededFuture(Collections.emptyList())); return; } AtomicLong c = new AtomicLong(0); ArrayList<T> l = new ArrayList<>(ids.size()); IntStream.range(0, ids.size()).forEach(i -> l.add(null)); ids.stream().forEach(e -> { getBlocking(e, r -> { l.set(ids.indexOf(e), r.result()); if (c.incrementAndGet() == ids.size()) { resultHandler.handle(Future.succeededFuture( l.stream().filter(s -> s != null).collect(Collectors.toCollection(ArrayList::new)))); } }); }); }
From source file:com.application.akscorp.yandextranslator2017.TranslateScreen.java
/** * @param data Record from history/*from www .j a v a2s .c om*/ * @param check set favourite status flag */ private void UpdateTranslateFavouriteStatus(ArrayList<String> data, boolean check) { String db_name = "Cache"; String TableName = "History"; ArrayList<MyPair<String, String>> cc = new ArrayList<>(); String fraze = data.get(2).replace("'", "''"); String Type = data.get(3); if (check) { cc.add(new MyPair<String, String>().mp("Favourite", "1")); data.set(5, "1"); } else { cc.add(new MyPair<String, String>().mp("Favourite", "0")); } db.UpdateDatabaseRecord(db_name, TableName, cc, new String[] { "text" }, "Input = '" + fraze + "' AND Type = '" + Type + "'"); //Update favourite and history list with changes myApp.getHistoryAndFavouriteScreen().HistoryData = db.GetRecordFromDatabase(db_name, TableName, "", -1, "CreateTime DESC"); myApp.getHistoryAndFavouriteScreen().FavouriteData = db.GetRecordFromDatabase(db_name, TableName, "Favourite = '1'", -1, "CreateTime DESC"); }
From source file:com.chinamobile.bcbsp.graph.GraphDataForDisk.java
/** * Set the bitmap's bit for the hashIndex hash bucket's nodeIndex headnode to * be 0./*from www. j a v a 2s. com*/ * @param hashIndex * @param nodeIndex * @param aBitmaps */ private void setBitmapFalse(int hashIndex, int nodeIndex, ArrayList<ArrayList<Integer>> aBitmaps) { // Get the refered bitmap of the bucket. ArrayList<Integer> bitmap = aBitmaps.get(hashIndex); // The node bit belongs to the point int element of the array. int point = nodeIndex / 32; // If the nodeIndex over the bitmap's size, append new 32 bit. if ((point + 1) > bitmap.size()) { bitmap.add(new Integer(0)); } // The bit shift number for the int element. int shift = 31 - nodeIndex % 32; // The unit for the int element to and. int andUnit = 1; andUnit = andUnit << shift; andUnit = ~andUnit; // And the int element with the unit to set the bit to 0. bitmap.set(point, bitmap.get(point) & andUnit); // Set back the bitmap into the bucket. aBitmaps.set(hashIndex, bitmap); }
From source file:com.hichinaschool.flashcards.libanki.Models.java
private Object[] _reqForTemplate(JSONObject m, ArrayList<String> flds, JSONObject t) { try {/*from w w w .j ava2 s .co m*/ ArrayList<String> a = new ArrayList<String>(); ArrayList<String> b = new ArrayList<String>(); for (String f : flds) { a.add("ankiflag"); b.add(""); } Object[] data; data = new Object[] { 1l, 1l, m.getLong("id"), 1l, t.getInt("ord"), "", Utils.joinFields(a.toArray(new String[a.size()])) }; String full = mCol._renderQA(data).get("q"); data = new Object[] { 1l, 1l, m.getLong("id"), 1l, t.getInt("ord"), "", Utils.joinFields(b.toArray(new String[b.size()])) }; String empty = mCol._renderQA(data).get("q"); // if full and empty are the same, the template is invalid and there is no way to satisfy it if (full.equals(empty)) { return new Object[] { "none", new JSONArray(), new JSONArray() }; } String type = "all"; JSONArray req = new JSONArray(); ArrayList<String> tmp = new ArrayList<String>(); for (int i = 0; i < flds.size(); i++) { tmp.clear(); tmp.addAll(a); tmp.set(i, ""); data[6] = Utils.joinFields(tmp.toArray(new String[tmp.size()])); // if no field content appeared, field is required if (!mCol._renderQA(data, new ArrayList<String>()).get("q").contains("ankiflag")) { req.put(i); } } if (req.length() > 0) { return new Object[] { type, req }; } // if there are no required fields, switch to any mode type = "any"; req = new JSONArray(); for (int i = 0; i < flds.size(); i++) { tmp.clear(); tmp.addAll(b); tmp.set(i, "1"); data[6] = Utils.joinFields(tmp.toArray(new String[tmp.size()])); // if not the same as empty, this field can make the card non-blank if (!mCol._renderQA(data).get("q").equals(empty)) { req.put(i); } } return new Object[] { type, req }; } catch (JSONException e) { throw new RuntimeException(e); } }
From source file:org.apache.pig.impl.plan.OperatorPlan.java
private MultiMap<E, E> generateNewMap(E oldNode, E newNode, MultiMap<E, E> mm) { // First, replace the key Collection<E> targets = mm.get(oldNode); if (targets != null) { mm.removeKey(oldNode);/*from w ww. j a v a2s. co m*/ mm.put(newNode, targets); } // We can't just do a remove and add in the map because of our // guarantee of not changing orders. So we need to walk the lists and // put the new node in the same slot as the old. // Walk all the other keys and replace any references to the oldNode // in their targets. MultiMap<E, E> newMap = new MultiMap<E, E>(mm.size()); for (E key : mm.keySet()) { Collection<E> c = mm.get(key); ArrayList<E> al = new ArrayList<E>(c); for (int i = 0; i < al.size(); i++) { if (al.get(i) == oldNode) al.set(i, newNode); } newMap.put(key, al); } return newMap; }
From source file:de.tud.kom.p2psim.impl.network.gnp.topology.GnpSpace.java
/** * //w ww . ja v a 2s . c om * @param noOfDimensions * number of Dimensions must be smaller than number of Monitors * @param monitorResheduling * number of rescheduling the downhill simplex * @param mapRef * reference to HostMap * @return optimized positions for Monitors */ private static GnpSpace getGnpWithDownhillSimplex(int noOfDimensions, int monitorResheduling, HostMap mapRef) { GnpSpace.calculationStepStatus = 1; GnpSpace.calculationInProgress = true; double alpha = 1.0; double beta = 0.5; double gamma = 2; double maxDiversity = 0.5; // N + 1 initial random Solutions int dhs_N = mapRef.getNoOfMonitors(); ArrayList<GnpSpace> solutions = new ArrayList<GnpSpace>(dhs_N + 1); for (int c = 0; c < dhs_N + 1; c++) solutions.add(new GnpSpace(noOfDimensions, mapRef)); // best and worst solution GnpSpace bestSolution = Collections.min(solutions); GnpSpace worstSolution = Collections.max(solutions); double bestError = bestSolution.getObjectiveValueMonitor(); double worstError = worstSolution.getObjectiveValueMonitor(); for (int z = 0; z < monitorResheduling; z++) { GnpSpace.calculationProgressStatus = z; // resheduling int count = 0; for (GnpSpace gnp : solutions) { if (gnp != bestSolution) { GnpPosition monitor = gnp.getMonitorPosition(count); monitor.diversify(gnp.getDimension(), maxDiversity); count++; } } // best and worst solution bestSolution = Collections.min(solutions); worstSolution = Collections.max(solutions); bestError = bestSolution.getObjectiveValueMonitor(); worstError = worstSolution.getObjectiveValueMonitor(); // stop criterion while (worstError - bestError > 0.00001 && calculationInProgress) { // move to center ... GnpSpace center = GnpSpace.getCenterSolution(solutions); GnpSpace newSolution1 = GnpSpace.getMovedSolution(worstSolution, center, 1 + alpha); double newError1 = newSolution1.getObjectiveValueMonitor(); if (newError1 <= bestError) { int IndexOfWorstSolution = solutions.indexOf(worstSolution); GnpSpace newSolution2 = GnpSpace.getMovedSolution(worstSolution, center, 1 + alpha + gamma); double newError2 = newSolution2.getObjectiveValueMonitor(); if (newError2 <= newError1) { solutions.set(IndexOfWorstSolution, newSolution2); bestError = newError2; } else { solutions.set(IndexOfWorstSolution, newSolution1); bestError = newError1; } bestSolution = solutions.get(IndexOfWorstSolution); } else if (newError1 < worstError) { int IndexOfWorstSolution = solutions.indexOf(worstSolution); solutions.set(IndexOfWorstSolution, newSolution1); } else { // ... or contract around best solution for (int c = 0; c < solutions.size(); c++) { if (solutions.get(c) != bestSolution) solutions.set(c, GnpSpace.getMovedSolution(solutions.get(c), bestSolution, beta)); } bestSolution = Collections.min(solutions); bestError = bestSolution.getObjectiveValueMonitor(); } worstSolution = Collections.max(solutions); worstError = worstSolution.getObjectiveValueMonitor(); } } // Set the Coordinate Reference to the Peer for (int c = 0; c < bestSolution.getNumberOfMonitors(); c++) { bestSolution.getMonitorPosition(c).getHostRef() .setPositionReference(bestSolution.getMonitorPosition(c)); } // GnpSpace.calculationStepStatus = 0; // GnpSpace.calculationInProgress = false; return bestSolution; }
From source file:eoss.problem.EOSSProblem.java
private void assertMissions(EOSSArchitecture arch) { try {// w ww . ja va 2 s . c o m for (int i = 0; i < EOSSDatabase.getOrbits().size(); i++) { Orbit orbit = EOSSDatabase.getOrbits().get(i); ArrayList<Instrument> instruments = arch.getInstrumentsInOrbit(orbit); if (instruments.size() > 0) { String payload = ""; double payloadMass = 0; double characteristicPower = 0; double dataRate = 0; ArrayList<Double> payloadDimensions = new ArrayList<>(); payloadDimensions.add(0, 0.0); //max dimension in x, y, and z payloadDimensions.add(1, 0.0); //nadir-area payloadDimensions.add(2, 0.0); //max z dimension String call = "(assert (MANIFEST::Mission (Name " + orbit + ") "; for (Instrument inst : instruments) { payload = payload + " " + inst.getName(); payloadMass += Double.parseDouble(inst.getProperty("mass#")); characteristicPower += Double.parseDouble(inst.getProperty("characteristic-power#")); dataRate += Double.parseDouble(inst.getProperty("average-data-rate#")); double dx = Double.parseDouble(inst.getProperty("dimension-x#")); double dy = Double.parseDouble(inst.getProperty("dimension-y#")); double dz = Double.parseDouble(inst.getProperty("dimension-z#")); payloadDimensions.set(0, Math.max(payloadDimensions.get(0), Math.max(Math.max(dx, dy), dz))); payloadDimensions.set(1, payloadDimensions.get(1) + dx * dy); payloadDimensions.set(2, Math.max(payloadDimensions.get(2), dz)); //manifest the instrument String callManifestInstrument = "(assert (CAPABILITIES::Manifested-instrument "; Iterator iter = inst.getProperties().iterator(); while (iter.hasNext()) { String propertyName = (String) iter.next(); callManifestInstrument += "(" + propertyName + " " + inst.getProperty(propertyName) + ")"; } callManifestInstrument += "(flies-in " + orbit.getName() + ")"; callManifestInstrument += "(orbit-altitude# " + String.valueOf((int) orbit.getAltitude()) + ")"; callManifestInstrument += "(orbit-inclination " + orbit.getInclination() + ")"; callManifestInstrument += "))"; r.eval(callManifestInstrument); } call += "(instruments " + payload + ") (launch-date 2015) (lifetime 5) (select-orbit no) " + orbit.toJessSlots(); call += "(payload-mass# " + String.valueOf(payloadMass) + ")"; call += "(payload-power# " + String.valueOf(characteristicPower) + ")"; call += "(payload-peak-power# " + String.valueOf(characteristicPower) + ")"; call += "(payload-data-rate# " + String.valueOf(dataRate) + ")"; double perOrbit = (dataRate * 1.2 * orbit.getPeriod()) / (1024 * 8); //(GByte/orbit) 20% overhead call += "(payload-dimensions# " + String.valueOf(payloadDimensions.get(0)) + " " + String.valueOf(payloadDimensions.get(1)) + " " + String.valueOf(payloadDimensions.get(2)) + ")"; call += "(sat-data-rate-per-orbit# " + String.valueOf(perOrbit) + ")"; call += "(num-of-sats-per-plane# " + String.valueOf(arch.getNumberOfSatellitesPerOrbit()) + ")))"; call += "(assert (SYNERGY::cross-registered-instruments " + " (instruments " + payload + ") (degree-of-cross-registration spacecraft) " + " (platform " + orbit + " )))"; r.eval(call); } } } catch (JessException ex) { Logger.getLogger(EOSSProblem.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:org.peerfact.impl.network.gnp.topology.GnpSpace.java
/** * /* w ww . jav a 2s . c o m*/ * @param noOfDimensions * number of Dimensions must be smaller than number of Monitors * @param monitorResheduling * number of rescheduling the downhill simplex * @param mapRef * reference to HostMap * @return optimized positions for Monitors */ private static GnpSpace getGnpWithDownhillSimplex(int noOfDimensions, int monitorResheduling, HostMap mapRef) { GnpSpace.calculationStepStatus = 1; GnpSpace.calculationInProgress = true; double alpha = 1.0; double beta = 0.5; double gamma = 2; double maxDiversity = 0.5; // N + 1 initial random Solutions int dhs_N = mapRef.getNoOfMonitors(); ArrayList<GnpSpace> solutions = new ArrayList<GnpSpace>(dhs_N + 1); for (int c = 0; c < dhs_N + 1; c++) { solutions.add(new GnpSpace(noOfDimensions, mapRef)); } // best and worst solution GnpSpace bestSolution = Collections.min(solutions); GnpSpace worstSolution = Collections.max(solutions); double bestError = bestSolution.getObjectiveValueMonitor(); double worstError = worstSolution.getObjectiveValueMonitor(); for (int z = 0; z < monitorResheduling; z++) { GnpSpace.calculationProgressStatus = z; // resheduling int count = 0; for (GnpSpace gnp : solutions) { if (gnp != bestSolution) { GnpPosition monitor = gnp.getMonitorPosition(count); monitor.diversify(gnp.getDimension(), maxDiversity); count++; } } // best and worst solution bestSolution = Collections.min(solutions); worstSolution = Collections.max(solutions); bestError = bestSolution.getObjectiveValueMonitor(); worstError = worstSolution.getObjectiveValueMonitor(); // stop criterion while (worstError - bestError > 0.00001 && calculationInProgress) { // move to center ... GnpSpace center = GnpSpace.getCenterSolution(solutions); GnpSpace newSolution1 = GnpSpace.getMovedSolution(worstSolution, center, 1 + alpha); double newError1 = newSolution1.getObjectiveValueMonitor(); if (newError1 <= bestError) { int IndexOfWorstSolution = solutions.indexOf(worstSolution); GnpSpace newSolution2 = GnpSpace.getMovedSolution(worstSolution, center, 1 + alpha + gamma); double newError2 = newSolution2.getObjectiveValueMonitor(); if (newError2 <= newError1) { solutions.set(IndexOfWorstSolution, newSolution2); bestError = newError2; } else { solutions.set(IndexOfWorstSolution, newSolution1); bestError = newError1; } bestSolution = solutions.get(IndexOfWorstSolution); } else if (newError1 < worstError) { int IndexOfWorstSolution = solutions.indexOf(worstSolution); solutions.set(IndexOfWorstSolution, newSolution1); } else { // ... or contract around best solution for (int c = 0; c < solutions.size(); c++) { if (solutions.get(c) != bestSolution) { solutions.set(c, GnpSpace.getMovedSolution(solutions.get(c), bestSolution, beta)); } } bestSolution = Collections.min(solutions); bestError = bestSolution.getObjectiveValueMonitor(); } worstSolution = Collections.max(solutions); worstError = worstSolution.getObjectiveValueMonitor(); } } // Set the Coordinate Reference to the Peer for (int c = 0; c < bestSolution.getNumberOfMonitors(); c++) { bestSolution.getMonitorPosition(c).getHostRef() .setPositionReference(bestSolution.getMonitorPosition(c)); } // GnpSpace.calculationStepStatus = 0; // GnpSpace.calculationInProgress = false; return bestSolution; }