List of usage examples for java.util Set equals
boolean equals(Object o);
From source file:com.dexcoder.dal.spring.mapper.JdbcRowMapper.java
/** * Extract the values for all columns in the current row. * <p>Utilizes public setters and result set metadata. * @see java.sql.ResultSetMetaData// ww w. j a va 2s. c om */ public T mapRow(ResultSet rs, int rowNumber) throws SQLException { Assert.state(this.mappedClass != null, "Mapped class was not specified"); T mappedObject = BeanUtils.instantiate(this.mappedClass); BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(mappedObject); initBeanWrapper(bw); ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount(); Set<String> populatedProperties = (isCheckFullyPopulated() ? new HashSet<String>() : null); for (int index = 1; index <= columnCount; index++) { String column = JdbcUtils.lookupColumnName(rsmd, index); String field = lowerCaseName(column.replaceAll(" ", "")); PropertyDescriptor pd = this.mappedFields.get(field); if (pd != null) { try { Object value = getColumnValue(rs, index, pd); if (rowNumber == 0 && logger.isDebugEnabled()) { logger.debug("Mapping column '" + column + "' to property '" + pd.getName() + "' of type [" + ClassUtils.getQualifiedName(pd.getPropertyType()) + "]"); } try { bw.setPropertyValue(pd.getName(), value); } catch (TypeMismatchException ex) { if (value == null && this.primitivesDefaultedForNullValue) { if (logger.isDebugEnabled()) { logger.debug("Intercepted TypeMismatchException for row " + rowNumber + " and column '" + column + "' with null value when setting property '" + pd.getName() + "' of type [" + ClassUtils.getQualifiedName(pd.getPropertyType()) + "] on object: " + mappedObject, ex); } } else { throw ex; } } if (populatedProperties != null) { populatedProperties.add(pd.getName()); } } catch (NotWritablePropertyException ex) { throw new DataRetrievalFailureException( "Unable to map column '" + column + "' to property '" + pd.getName() + "'", ex); } } else { // No PropertyDescriptor found if (rowNumber == 0 && logger.isDebugEnabled()) { logger.debug("No property found for column '" + column + "' mapped to field '" + field + "'"); } } } if (populatedProperties != null && !populatedProperties.equals(this.mappedProperties)) { throw new InvalidDataAccessApiUsageException( "Given ResultSet does not contain all fields " + "necessary to populate object of class [" + this.mappedClass.getName() + "]: " + this.mappedProperties); } return mappedObject; }
From source file:org.springframework.jdbc.core.BeanPropertyRowMapper.java
/** * Extract the values for all columns in the current row. * <p>Utilizes public setters and result set metadata. * @see java.sql.ResultSetMetaData/*w ww. j a v a2s . c o m*/ */ @Override public T mapRow(ResultSet rs, int rowNumber) throws SQLException { Assert.state(this.mappedClass != null, "Mapped class was not specified"); T mappedObject = BeanUtils.instantiateClass(this.mappedClass); BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(mappedObject); initBeanWrapper(bw); ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount(); Set<String> populatedProperties = (isCheckFullyPopulated() ? new HashSet<>() : null); for (int index = 1; index <= columnCount; index++) { String column = JdbcUtils.lookupColumnName(rsmd, index); String field = lowerCaseName(column.replaceAll(" ", "")); PropertyDescriptor pd = (this.mappedFields != null ? this.mappedFields.get(field) : null); if (pd != null) { try { Object value = getColumnValue(rs, index, pd); if (rowNumber == 0 && logger.isDebugEnabled()) { logger.debug("Mapping column '" + column + "' to property '" + pd.getName() + "' of type '" + ClassUtils.getQualifiedName(pd.getPropertyType()) + "'"); } try { bw.setPropertyValue(pd.getName(), value); } catch (TypeMismatchException ex) { if (value == null && this.primitivesDefaultedForNullValue) { if (logger.isDebugEnabled()) { logger.debug("Intercepted TypeMismatchException for row " + rowNumber + " and column '" + column + "' with null value when setting property '" + pd.getName() + "' of type '" + ClassUtils.getQualifiedName(pd.getPropertyType()) + "' on object: " + mappedObject, ex); } } else { throw ex; } } if (populatedProperties != null) { populatedProperties.add(pd.getName()); } } catch (NotWritablePropertyException ex) { throw new DataRetrievalFailureException( "Unable to map column '" + column + "' to property '" + pd.getName() + "'", ex); } } else { // No PropertyDescriptor found if (rowNumber == 0 && logger.isDebugEnabled()) { logger.debug("No property found for column '" + column + "' mapped to field '" + field + "'"); } } } if (populatedProperties != null && !populatedProperties.equals(this.mappedProperties)) { throw new InvalidDataAccessApiUsageException( "Given ResultSet does not contain all fields " + "necessary to populate object of class [" + this.mappedClass.getName() + "]: " + this.mappedProperties); } return mappedObject; }
From source file:org.apache.asterix.test.aql.TestExecutor.java
private boolean equalStrings(String expected, String actual, boolean regexMatch) { String[] rowsExpected = expected.split("\n"); String[] rowsActual = actual.split("\n"); for (int i = 0; i < rowsExpected.length; i++) { String expectedRow = rowsExpected[i]; String actualRow = rowsActual[i]; if (regexMatch) { if (actualRow.matches(expectedRow)) { continue; }//ww w.ja v a 2 s. c o m } else if (actualRow.equals(expectedRow)) { continue; } String[] expectedFields = expectedRow.split(" "); String[] actualFields = actualRow.split(" "); boolean bagEncountered = false; Set<String> expectedBagElements = new HashSet<>(); Set<String> actualBagElements = new HashSet<>(); for (int j = 0; j < expectedFields.length; j++) { if (j >= actualFields.length) { return false; } else if (expectedFields[j].equals(actualFields[j])) { bagEncountered = expectedFields[j].equals("{{"); if (expectedFields[j].startsWith("}}")) { if (regexMatch) { if (expectedBagElements.size() != actualBagElements.size()) { return false; } int[] expectedHits = new int[expectedBagElements.size()]; int[] actualHits = new int[actualBagElements.size()]; int k = 0; for (String expectedElement : expectedBagElements) { int l = 0; for (String actualElement : actualBagElements) { if (actualElement.matches(expectedElement)) { expectedHits[k]++; actualHits[l]++; } l++; } k++; } for (int m = 0; m < expectedHits.length; m++) { if (expectedHits[m] == 0 || actualHits[m] == 0) { return false; } } } else if (!expectedBagElements.equals(actualBagElements)) { return false; } bagEncountered = false; expectedBagElements.clear(); actualBagElements.clear(); } } else if (expectedFields[j].indexOf('.') < 0) { if (bagEncountered) { expectedBagElements.add(expectedFields[j].replaceAll(",$", "")); actualBagElements.add(actualFields[j].replaceAll(",$", "")); continue; } return false; } else { // If the fields are floating-point numbers, test them // for equality safely expectedFields[j] = expectedFields[j].split(",")[0]; actualFields[j] = actualFields[j].split(",")[0]; try { Double double1 = Double.parseDouble(expectedFields[j]); Double double2 = Double.parseDouble(actualFields[j]); float float1 = (float) double1.doubleValue(); float float2 = (float) double2.doubleValue(); if (Math.abs(float1 - float2) == 0) { continue; } else { return false; } } catch (NumberFormatException ignored) { // Guess they weren't numbers - must simply not be equal return false; } } } } return true; }
From source file:org.jboss.forge.roaster.model.impl.AnnotationImpl.java
@Override public AnnotationSource<O> removeAnnotationValue(String name, org.jboss.forge.roaster.model.Annotation<O> element) { requireNonNull(element, "Cannot remove null element"); if (isSingleValue() && Objects.equals(name, DEFAULT_VALUE)) { return removeAnnotationValue(element); }/*from w ww. j a va2 s. c o m*/ if (isNormal()) { final Set<String> identifiers = new HashSet<>(); for (@SuppressWarnings("unchecked") Iterator<Object> values = ((NormalAnnotation) annotation).values().iterator(); values.hasNext();) { final Object value = values.next(); if (value instanceof MemberValuePair) { final String identifier = ((MemberValuePair) value).getName().getIdentifier(); identifiers.add(identifier); if (Objects.equals(name, identifier)) { Expression expr = ((MemberValuePair) value).getValue(); if (element.getInternal().equals(expr)) { // remove entire annotation element for inlined single-element array: values.remove(); identifiers.remove(identifier); continue; } if (expr instanceof ArrayInitializer) { final ArrayInitializer arrayInit = (ArrayInitializer) expr; // remove element: arrayInit.expressions().remove(element.getInternal()); if (arrayInit.expressions().isEmpty()) { // remove empty array: values.remove(); identifiers.remove(identifier); } else if (arrayInit.expressions().size() == 1) { // promote single-element array: ((MemberValuePair) value).setValue((Expression) ASTNode.copySubtree(ast, (ASTNode) arrayInit.expressions().get(0))); } } } } } // finally, reduce to simplest equivalent annotation type: if (identifiers.isEmpty()) { convertTo(AnnotationType.MARKER); } else if (identifiers.equals(Collections.singleton(DEFAULT_VALUE))) { convertTo(AnnotationType.SINGLE); } } return this; }
From source file:org.apache.apex.malhar.lib.io.fs.AbstractFileInputOperatorTest.java
@Test public void testEmptyDirectory() throws Exception { FileContext.getLocalFSFileContext().delete(new Path(new File(testMeta.dir).getAbsolutePath()), true); Set<String> dPaths = Sets.newHashSet(); dPaths.add(new File(testMeta.dir).getCanonicalPath()); String subdir01 = "/a"; dPaths.add(new File(testMeta.dir + subdir01).getCanonicalPath()); FileUtils.forceMkdir((new File(testMeta.dir + subdir01))); String subdir02 = "/b"; dPaths.add(new File(testMeta.dir + subdir02).getCanonicalPath()); FileUtils.forceMkdir(new File(testMeta.dir + subdir02)); String subdir03 = subdir02 + "/c"; dPaths.add(new File(testMeta.dir + subdir03).getCanonicalPath()); FileUtils.forceMkdir(new File(testMeta.dir + subdir03)); String subdir04 = "/d"; List<String> allLines = Lists.newArrayList(); HashSet<String> lines = Sets.newHashSet(); for (int line = 0; line < 5; line++) { lines.add("f0" + "l" + line); }//from w ww . j a v a2 s.c o m allLines.addAll(lines); File testFile = new File(testMeta.dir + subdir04, "file0"); dPaths.add(new File(testMeta.dir + subdir04).getCanonicalPath()); FileUtils.write(testFile, StringUtils.join(lines, '\n')); LineOperator oper = new LineOperator(); oper.setDirectory(new File(testMeta.dir).getAbsolutePath()); oper.setScanIntervalMillis(0); CollectorTestSink<String> queryResults = new CollectorTestSink<String>(); @SuppressWarnings({ "unchecked", "rawtypes" }) CollectorTestSink<Object> sink = (CollectorTestSink) queryResults; oper.output.setSink(sink); int wid = 0; // Read all records to populate processedList in operator. oper.setup(testMeta.context); for (int i = 0; i < 3; i++) { oper.beginWindow(wid); oper.emitTuples(); oper.endWindow(); wid++; } Assert.assertEquals("Size", 5, oper.dirPaths.size()); Assert.assertTrue("Checking Sets", dPaths.equals(oper.dirPaths)); }
From source file:com.insframework.common.spring.jdbc.mapper.BeanPropertyRowMapper.java
/** * Extract the values for all columns in the current row. * <p>Utilizes public setters and result set metadata. * @see java.sql.ResultSetMetaData/*from w w w.j a v a 2 s . c o m*/ */ @Override public T mapRow(ResultSet rs, int rowNumber) throws SQLException { Assert.state(this.mappedClass != null, "Mapped class was not specified"); T mappedObject = BeanUtils.instantiate(this.mappedClass); BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(mappedObject); initBeanWrapper(bw); ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount(); Set<String> populatedProperties = (isCheckFullyPopulated() ? new HashSet<String>() : null); for (int index = 1; index <= columnCount; index++) { String column = JdbcUtils.lookupColumnName(rsmd, index); PropertyDescriptor pd = this.mappedFields.get(column.replaceAll(" ", "").toLowerCase()); if (pd != null) { try { Object value = getColumnValue(rs, index, pd); if (logger.isDebugEnabled() && rowNumber == 0) { logger.debug("Mapping column '" + column + "' to property '" + pd.getName() + "' of type " + pd.getPropertyType()); } try { //add by guom if (pd.getPropertyType() != null && "java.lang.String".equals(pd.getPropertyType().getName())) { if (value != null) { bw.setPropertyValue(pd.getName(), String.valueOf(value)); } else { bw.setPropertyValue(pd.getName(), ""); } } else if (pd.getPropertyType() != null && "double".equals(pd.getPropertyType().getName())) { if (value != null) { bw.setPropertyValue(pd.getName(), value); } else { bw.setPropertyValue(pd.getName(), 0d); } } else { bw.setPropertyValue(pd.getName(), value); } } catch (TypeMismatchException e) { if (value == null && primitivesDefaultedForNullValue) { logger.info("Intercepted TypeMismatchException for row " + rowNumber + " and column '" + column + "' with value " + value + " when setting property '" + pd.getName() + "' of type " + pd.getPropertyType() + " on object: " + mappedObject); } else { throw e; } } if (populatedProperties != null) { populatedProperties.add(pd.getName()); } } catch (NotWritablePropertyException ex) { throw new DataRetrievalFailureException( "Unable to map column " + column + " to property " + pd.getName(), ex); } } } if (populatedProperties != null && !populatedProperties.equals(this.mappedProperties)) { throw new InvalidDataAccessApiUsageException("Given ResultSet does not contain all fields " + "necessary to populate object of class [" + this.mappedClass + "]: " + this.mappedProperties); } return mappedObject; }
From source file:com.mirth.connect.cli.CommandLineInterface.java
private void importChannelDependencies(Channel importChannel) throws ClientException { if (CollectionUtils.isNotEmpty(importChannel.getDependentIds()) || CollectionUtils.isNotEmpty(importChannel.getDependencyIds())) { Set<ChannelDependency> cachedChannelDependencies = client.getChannelDependencies(); Set<ChannelDependency> channelDependencies = new HashSet<ChannelDependency>(cachedChannelDependencies); if (CollectionUtils.isNotEmpty(importChannel.getDependentIds())) { for (String dependentId : importChannel.getDependentIds()) { if (StringUtils.isNotBlank(dependentId) && !StringUtils.equals(dependentId, importChannel.getId())) { channelDependencies.add(new ChannelDependency(dependentId, importChannel.getId())); }/*from ww w . ja va 2 s . co m*/ } } if (CollectionUtils.isNotEmpty(importChannel.getDependencyIds())) { for (String dependencyId : importChannel.getDependencyIds()) { if (StringUtils.isNotBlank(dependencyId) && !StringUtils.equals(dependencyId, importChannel.getId())) { channelDependencies.add(new ChannelDependency(importChannel.getId(), dependencyId)); } } } if (!channelDependencies.equals(cachedChannelDependencies)) { try { client.setChannelDependencies(channelDependencies); } catch (ClientException e) { error("Unable to save channel dependencies.", e); } } importChannel.clearDependencies(); } }
From source file:org.apache.accumulo.tserver.tablet.Tablet.java
private CompactionStats _majorCompact(MajorCompactionReason reason) throws IOException, CompactionCanceledException { long t1, t2, t3; Pair<Long, UserCompactionConfig> compactionId = null; CompactionStrategy strategy = null;/* www .j a v a 2 s . c om*/ Map<FileRef, Pair<Key, Key>> firstAndLastKeys = null; if (reason == MajorCompactionReason.USER) { try { compactionId = getCompactionID(); strategy = createCompactionStrategy(compactionId.getSecond().getCompactionStrategy()); } catch (NoNodeException e) { throw new RuntimeException(e); } } else if (reason == MajorCompactionReason.NORMAL || reason == MajorCompactionReason.IDLE) { strategy = Property.createTableInstanceFromPropertyName(tableConfiguration, Property.TABLE_COMPACTION_STRATEGY, CompactionStrategy.class, new DefaultCompactionStrategy()); strategy.init(Property.getCompactionStrategyOptions(tableConfiguration)); } else if (reason == MajorCompactionReason.CHOP) { firstAndLastKeys = getFirstAndLastKeys(getDatafileManager().getDatafileSizes()); } else { throw new IllegalArgumentException("Unknown compaction reason " + reason); } if (strategy != null) { MajorCompactionRequest request = new MajorCompactionRequest(extent, reason, getTabletServer().getFileSystem(), tableConfiguration); request.setFiles(getDatafileManager().getDatafileSizes()); strategy.gatherInformation(request); } Map<FileRef, DataFileValue> filesToCompact = null; int maxFilesToCompact = tableConfiguration.getCount(Property.TSERV_MAJC_THREAD_MAXOPEN); CompactionStats majCStats = new CompactionStats(); CompactionPlan plan = null; boolean propogateDeletes = false; boolean updateCompactionID = false; synchronized (this) { // plan all that work that needs to be done in the sync block... then do the actual work // outside the sync block t1 = System.currentTimeMillis(); majorCompactionState = CompactionState.WAITING_TO_START; getTabletMemory().waitForMinC(); t2 = System.currentTimeMillis(); majorCompactionState = CompactionState.IN_PROGRESS; notifyAll(); VolumeManager fs = getTabletServer().getFileSystem(); if (extent.isRootTablet()) { // very important that we call this before doing major compaction, // otherwise deleted compacted files could possible be brought back // at some point if the file they were compacted to was legitimately // removed by a major compaction RootFiles.cleanupReplacement(fs, fs.listStatus(this.location), false); } SortedMap<FileRef, DataFileValue> allFiles = getDatafileManager().getDatafileSizes(); List<FileRef> inputFiles = new ArrayList<FileRef>(); if (reason == MajorCompactionReason.CHOP) { // enforce rules: files with keys outside our range need to be compacted inputFiles.addAll(findChopFiles(extent, firstAndLastKeys, allFiles.keySet())); } else { MajorCompactionRequest request = new MajorCompactionRequest(extent, reason, fs, tableConfiguration); request.setFiles(allFiles); plan = strategy.getCompactionPlan(request); if (plan != null) { plan.validate(allFiles.keySet()); inputFiles.addAll(plan.inputFiles); } } if (inputFiles.isEmpty()) { if (reason == MajorCompactionReason.USER) { if (compactionId.getSecond().getIterators().isEmpty()) { log.debug("No-op major compaction by USER on 0 input files because no iterators present."); lastCompactID = compactionId.getFirst(); updateCompactionID = true; } else { log.debug("Major compaction by USER on 0 input files with iterators."); filesToCompact = new HashMap<>(); } } else { return majCStats; } } else { // If no original files will exist at the end of the compaction, we do not have to propogate deletes Set<FileRef> droppedFiles = new HashSet<>(); droppedFiles.addAll(inputFiles); if (plan != null) droppedFiles.addAll(plan.deleteFiles); propogateDeletes = !(droppedFiles.equals(allFiles.keySet())); log.debug("Major compaction plan: " + plan + " propogate deletes : " + propogateDeletes); filesToCompact = new HashMap<>(allFiles); filesToCompact.keySet().retainAll(inputFiles); getDatafileManager().reserveMajorCompactingFiles(filesToCompact.keySet()); } t3 = System.currentTimeMillis(); } try { log.debug(String.format("MajC initiate lock %.2f secs, wait %.2f secs", (t3 - t2) / 1000.0, (t2 - t1) / 1000.0)); if (updateCompactionID) { MetadataTableUtil.updateTabletCompactID(extent, compactionId.getFirst(), tabletServer, getTabletServer().getLock()); return majCStats; } if (!propogateDeletes && compactionId == null) { // compacting everything, so update the compaction id in metadata try { compactionId = getCompactionID(); if (compactionId.getSecond().getCompactionStrategy() != null) { compactionId = null; // TODO maybe return unless chop? } } catch (NoNodeException e) { throw new RuntimeException(e); } } List<IteratorSetting> compactionIterators = new ArrayList<IteratorSetting>(); if (compactionId != null) { if (reason == MajorCompactionReason.USER) { if (getCompactionCancelID() >= compactionId.getFirst()) { // compaction was canceled return majCStats; } compactionIterators = compactionId.getSecond().getIterators(); synchronized (this) { if (lastCompactID >= compactionId.getFirst()) // already compacted return majCStats; } } } // need to handle case where only one file is being major compacted // ACCUMULO-3645 run loop at least once, even if filesToCompact.isEmpty() do { int numToCompact = maxFilesToCompact; if (filesToCompact.size() > maxFilesToCompact && filesToCompact.size() < 2 * maxFilesToCompact) { // on the second to last compaction pass, compact the minimum amount of files possible numToCompact = filesToCompact.size() - maxFilesToCompact + 1; } Set<FileRef> smallestFiles = removeSmallest(filesToCompact, numToCompact); FileRef fileName = getNextMapFilename( (filesToCompact.size() == 0 && !propogateDeletes) ? "A" : "C"); FileRef compactTmpName = new FileRef(fileName.path().toString() + "_tmp"); AccumuloConfiguration tableConf = createTableConfiguration(tableConfiguration, plan); Span span = Trace.start("compactFiles"); try { CompactionEnv cenv = new CompactionEnv() { @Override public boolean isCompactionEnabled() { return Tablet.this.isCompactionEnabled(); } @Override public IteratorScope getIteratorScope() { return IteratorScope.majc; } @Override public RateLimiter getReadLimiter() { return getTabletServer().getMajorCompactionReadLimiter(); } @Override public RateLimiter getWriteLimiter() { return getTabletServer().getMajorCompactionWriteLimiter(); } }; HashMap<FileRef, DataFileValue> copy = new HashMap<FileRef, DataFileValue>( getDatafileManager().getDatafileSizes()); if (!copy.keySet().containsAll(smallestFiles)) throw new IllegalStateException("Cannot find data file values for " + smallestFiles); copy.keySet().retainAll(smallestFiles); log.debug("Starting MajC " + extent + " (" + reason + ") " + copy.keySet() + " --> " + compactTmpName + " " + compactionIterators); // always propagate deletes, unless last batch boolean lastBatch = filesToCompact.isEmpty(); Compactor compactor = new Compactor(tabletServer, this, copy, null, compactTmpName, lastBatch ? propogateDeletes : true, cenv, compactionIterators, reason.ordinal(), tableConf); CompactionStats mcs = compactor.call(); span.data("files", "" + smallestFiles.size()); span.data("read", "" + mcs.getEntriesRead()); span.data("written", "" + mcs.getEntriesWritten()); majCStats.add(mcs); if (lastBatch && plan != null && plan.deleteFiles != null) { smallestFiles.addAll(plan.deleteFiles); } getDatafileManager().bringMajorCompactionOnline(smallestFiles, compactTmpName, fileName, filesToCompact.size() == 0 && compactionId != null ? compactionId.getFirst() : null, new DataFileValue(mcs.getFileSize(), mcs.getEntriesWritten())); // when major compaction produces a file w/ zero entries, it will be deleted... do not want // to add the deleted file if (filesToCompact.size() > 0 && mcs.getEntriesWritten() > 0) { filesToCompact.put(fileName, new DataFileValue(mcs.getFileSize(), mcs.getEntriesWritten())); } } finally { span.stop(); } } while (filesToCompact.size() > 0); return majCStats; } finally { synchronized (Tablet.this) { getDatafileManager().clearMajorCompactingFile(); } } }
From source file:de.schildbach.pte.AbstractNavitiaProvider.java
@Override public QueryTripsResult queryTrips(final Location from, final @Nullable Location via, final Location to, final Date date, final boolean dep, final @Nullable Set<Product> products, final @Nullable Optimize optimize, final @Nullable WalkSpeed walkSpeed, final @Nullable Accessibility accessibility, final @Nullable Set<Option> options) throws IOException { final ResultHeader resultHeader = new ResultHeader(network, SERVER_PRODUCT, SERVER_VERSION, null, 0, null); try {/* w ww . j a v a 2s. co m*/ if (from != null && from.isIdentified() && to != null && to.isIdentified()) { final HttpUrl.Builder url = apiBase.newBuilder().addPathSegment("journeys"); url.addQueryParameter("from", printLocation(from)); url.addQueryParameter("to", printLocation(to)); url.addQueryParameter("datetime", printDate(date)); url.addQueryParameter("datetime_represents", dep ? "departure" : "arrival"); url.addQueryParameter("min_nb_journeys", Integer.toString(this.numTripsRequested)); url.addQueryParameter("depth", "0"); // Set walking speed. if (walkSpeed != null) { final double walkingSpeed; switch (walkSpeed) { case SLOW: walkingSpeed = 1.12 * 0.8; break; case FAST: walkingSpeed = 1.12 * 1.2; break; case NORMAL: default: walkingSpeed = 1.12; break; } url.addQueryParameter("walking_speed", Double.toString(walkingSpeed)); } if (options != null && options.contains(Option.BIKE)) { url.addQueryParameter("first_section_mode", "bike"); url.addQueryParameter("last_section_mode", "bike"); } // Set forbidden physical modes. if (products != null && !products.equals(Product.ALL)) { url.addQueryParameter("forbidden_uris[]", "physical_mode:Air"); url.addQueryParameter("forbidden_uris[]", "physical_mode:Boat"); if (!products.contains(Product.REGIONAL_TRAIN)) { url.addQueryParameter("forbidden_uris[]", "physical_mode:Localdistancetrain"); url.addQueryParameter("forbidden_uris[]", "physical_mode:Train"); } if (!products.contains(Product.SUBURBAN_TRAIN)) { url.addQueryParameter("forbidden_uris[]", "physical_mode:Localtrain"); url.addQueryParameter("forbidden_uris[]", "physical_mode:Train"); url.addQueryParameter("forbidden_uris[]", "physical_mode:Rapidtransit"); } if (!products.contains(Product.SUBWAY)) { url.addQueryParameter("forbidden_uris[]", "physical_mode:Metro"); } if (!products.contains(Product.TRAM)) { url.addQueryParameter("forbidden_uris[]", "physical_mode:Tramway"); } if (!products.contains(Product.BUS)) { url.addQueryParameter("forbidden_uris[]", "physical_mode:Bus"); url.addQueryParameter("forbidden_uris[]", "physical_mode:Busrapidtransit"); url.addQueryParameter("forbidden_uris[]", "physical_mode:Coach"); url.addQueryParameter("forbidden_uris[]", "physical_mode:Shuttle"); } if (!products.contains(Product.FERRY)) { url.addQueryParameter("forbidden_uris[]", "physical_mode:Ferry"); } if (!products.contains(Product.CABLECAR)) { url.addQueryParameter("forbidden_uris[]", "physical_mode:Funicular"); } if (!products.contains(Product.ON_DEMAND)) { url.addQueryParameter("forbidden_uris[]", "physical_mode:Taxi"); } } final CharSequence page = httpClient.get(url.build()); try { final JSONObject head = new JSONObject(page.toString()); if (head.has("error")) { final JSONObject error = head.getJSONObject("error"); final String id = error.getString("id"); if (id.equals("no_solution")) return new QueryTripsResult(resultHeader, QueryTripsResult.Status.NO_TRIPS); else throw new IllegalArgumentException("Unhandled error id: " + id); } else { // Fill context. HttpUrl prevQueryUrl = null; HttpUrl nextQueryUrl = null; final JSONArray links = head.getJSONArray("links"); for (int i = 0; i < links.length(); ++i) { final JSONObject link = links.getJSONObject(i); final String type = link.getString("type"); if (type.equals("prev")) { prevQueryUrl = HttpUrl.parse(link.getString("href")); } else if (type.equals("next")) { nextQueryUrl = HttpUrl.parse(link.getString("href")); } } String prevQueryUrlString = prevQueryUrl != null ? prevQueryUrl.toString() : null; String nextQueryUrlString = nextQueryUrl != null ? nextQueryUrl.toString() : null; final QueryTripsResult result = new QueryTripsResult(resultHeader, url.build().toString(), from, null, to, new Context(from, to, prevQueryUrlString, nextQueryUrlString), new LinkedList<Trip>()); parseQueryTripsResult(head, from, to, result); return result; } } catch (final JSONException jsonExc) { throw new ParserException(jsonExc); } } else if (from != null && to != null) { List<Location> ambiguousFrom = null, ambiguousTo = null; Location newFrom = null, newTo = null; if (!from.isIdentified() && from.hasName()) { ambiguousFrom = suggestLocations(from.name).getLocations(); if (ambiguousFrom.isEmpty()) return new QueryTripsResult(resultHeader, QueryTripsResult.Status.UNKNOWN_FROM); if (ambiguousFrom.size() == 1 && ambiguousFrom.get(0).isIdentified()) newFrom = ambiguousFrom.get(0); } if (!to.isIdentified() && to.hasName()) { ambiguousTo = suggestLocations(to.name).getLocations(); if (ambiguousTo.isEmpty()) return new QueryTripsResult(resultHeader, QueryTripsResult.Status.UNKNOWN_TO); if (ambiguousTo.size() == 1 && ambiguousTo.get(0).isIdentified()) newTo = ambiguousTo.get(0); } if (newTo != null && newFrom != null) return queryTrips(newFrom, via, newTo, date, dep, products, optimize, walkSpeed, accessibility, options); if (ambiguousFrom != null || ambiguousTo != null) return new QueryTripsResult(resultHeader, ambiguousFrom, null, ambiguousTo); } return new QueryTripsResult(resultHeader, QueryTripsResult.Status.NO_TRIPS); } catch (final NotFoundException fnfExc) { try { final JSONObject head = new JSONObject(fnfExc.getBodyPeek().toString()); final JSONObject error = head.getJSONObject("error"); final String id = error.getString("id"); if (id.equals("unknown_object")) { // Identify unknown object. final String fromString = printLocation(from); final String toString = printLocation(to); final String message = error.getString("message"); if (message.equals("Invalid id : " + fromString)) return new QueryTripsResult(resultHeader, QueryTripsResult.Status.UNKNOWN_FROM); else if (message.equals("Invalid id : " + toString)) return new QueryTripsResult(resultHeader, QueryTripsResult.Status.UNKNOWN_TO); else throw new IllegalArgumentException("Unhandled error message: " + message); } else if (id.equals("date_out_of_bounds")) { return new QueryTripsResult(resultHeader, QueryTripsResult.Status.INVALID_DATE); } else { throw new IllegalArgumentException("Unhandled error id: " + id); } } catch (final JSONException jsonExc) { throw new ParserException("Cannot parse error content, original exception linked", fnfExc); } } }
From source file:ddf.catalog.CatalogFrameworkImplTest.java
@Test public void testGetAllSiteNames() { String frameworkName = "DDF"; CatalogProvider provider = new MockMemoryProvider("Provider", "Provider", "v1.0", "DDF", new HashSet<ContentType>(), true, new Date()); List<FederatedSource> federatedSources = createDefaultFederatedSourceList(true); // Expected Set of Names Set<String> expectedNameSet = new HashSet<String>(); expectedNameSet.add(frameworkName);/*from w w w . j a v a 2 s.com*/ for (FederatedSource curSite : federatedSources) { expectedNameSet.add(curSite.getId()); } // Mock register the provider in the container // Mock the source poller SourcePoller mockPoller = mock(SourcePoller.class); when(mockPoller.isAvailable(isA(Source.class))).thenReturn(Boolean.TRUE); CatalogFrameworkImpl framework = new CatalogFrameworkImpl(Collections.singletonList(provider), null, new ArrayList<PreIngestPlugin>(), new ArrayList<PostIngestPlugin>(), new ArrayList<PreQueryPlugin>(), new ArrayList<PostQueryPlugin>(), new ArrayList<PreResourcePlugin>(), new ArrayList<PostResourcePlugin>(), new ArrayList<ConnectedSource>(), federatedSources, new ArrayList<ResourceReader>(), null, null, mockPoller); framework.bind(provider); framework.setId(frameworkName); // Returned Set of Names // Returned Sites SourceInfoRequest request = new SourceInfoRequestEnterprise(true); SourceInfoResponse response = null; try { response = framework.getSourceInfo(request); } catch (SourceUnavailableException e) { LOGGER.debug("SourceUnavilable", e); fail(); } assert (response != null); Set<SourceDescriptor> sourceDescriptors = response.getSourceInfo(); // should contain ONLY the original federated sites assertEquals(expectedNameSet.size(), sourceDescriptors.size()); Set<String> returnedSourceIds = new HashSet<String>(); for (SourceDescriptor sd : sourceDescriptors) { returnedSourceIds.add(sd.getSourceId()); } for (String id : returnedSourceIds) { LOGGER.debug("returned sourceId: " + id); } assertTrue(expectedNameSet.equals(returnedSourceIds)); }