List of usage examples for org.apache.commons.collections CollectionUtils get
public static Object get(Object object, int index)
index
-th value in object
, throwing IndexOutOfBoundsException
if there is no such element or IllegalArgumentException
if object
is not an instance of one of the supported types. From source file:net.big_oh.common.utils.CollectionsUtil.java
/** * //from ww w. j a va 2s . co m * @param <T> * @param originalSet * The set of original objects from which combinations of size k * will be generated. * @param k * The size of combinations to be generated. * @return Returns the set of all size k sets (or combinations) that can be * constructed from the originalSet * @throws IllegalArgumentException * thrown if k is less than zero of greater than the size of the * original set. */ @SuppressWarnings("unchecked") public static <T> Set<Set<T>> getCombinations(Set<T> originalSet, int k) throws IllegalArgumentException { // sanity check if (k < 0) { throw new IllegalArgumentException("The value of the k parameter cannot be less than zero."); } if (k > originalSet.size()) { throw new IllegalArgumentException( "The value of the k parameter cannot be greater than the size of originalSet."); } // base case ... k == 0 if (k == 0) { Set<Set<T>> combinations = new HashSet<Set<T>>(); combinations.add(new HashSet<T>()); return combinations; } // base case ... k == 1 if (k == 1) { Set<Set<T>> combinations = new HashSet<Set<T>>(); for (T t : originalSet) { Set<T> combination = new HashSet<T>(); combination.add(t); combinations.add(combination); } return combinations; } // recursive case T arbitraryElement = (T) CollectionUtils.get(originalSet, 0); Set<T> everythingElse = new HashSet<T>(originalSet); everythingElse.remove(arbitraryElement); Set<Set<T>> combinations = new HashSet<Set<T>>(); // pair arbitraryElelement with combinations of size k-1 from the // everythingElse collection for (Set<T> combinationForEverythingElse : getCombinations(everythingElse, k - 1)) { combinationForEverythingElse.add(arbitraryElement); combinations.add(combinationForEverythingElse); } // get combinations of size k from the everythingElse collection if (k <= everythingElse.size()) { for (Set<T> combinationForEverythingElse : getCombinations(everythingElse, k)) { combinations.add(combinationForEverythingElse); } } return combinations; }
From source file:hr.fer.zemris.vhdllab.service.workspace.ProjectMetadataTest.java
@Test public void afterSerialization() { metadata = (ProjectMetadata) SerializationUtils.clone(metadata); for (File f : metadata.getFiles()) { assertEquals(hierarchy.getProject(), f.getProject()); }//from w w w .j a v a 2s . co m File file = (File) CollectionUtils.get(files, 0); assertTrue( "change in hashCode of a file (added project reference) isn't reflected to the set containing it.", metadata.getFiles().contains(file)); }
From source file:de.extrastandard.persistence.model.ExecutionIT.java
@Test public void testInputDataSucessPhase2AnotherTransaction() { final List<ICommunicationProtocol> sourceInputDataList = executionPersistence .findInputDataForExecution(PersistenceTestSetup.PROCEDURE_DATA_MATCH_NAME, PhaseQualifier.PHASE2); Assert.assertTrue("InputData is empty", !sourceInputDataList.isEmpty()); final IExecution execution = executionPersistence .startExecution(PersistenceTestSetup.PROCEDURE_DATA_MATCH_NAME, "Test", PhaseQualifier.PHASE2); final IResponseData responseData = new ResponseData(); final DbQueryInputDataContainer dbQueryInputData = new DbQueryInputDataContainer(); final String testRequestId = "TEST_REQUEST_ID"; final String returnCode = "return code phase 2"; final String returnText = "return text phase2"; final String responseId = "response id phase 2"; for (final ICommunicationProtocol iSourceInputData : sourceInputDataList) { final Long sourceInputDataId = iSourceInputData.getId(); final String inputDataRequestId = testRequestId + sourceInputDataId; final String inputDataReturnCode = returnCode + sourceInputDataId; final String inputDataReturnText = returnText + sourceInputDataId; final String inputDataResponseId = responseId + sourceInputDataId; final Boolean successful = true; final ISingleResponseData singleResponseData = new SingleResponseData(inputDataRequestId, inputDataReturnCode, inputDataReturnText, inputDataResponseId, successful, PersistentStatus.DONE, "Output-ID"); responseData.addSingleResponse(singleResponseData); final IDbQueryInputData singleQueryInputData = new DbQueryInputData(sourceInputDataId, iSourceInputData.getRequestId(), iSourceInputData.getResponseId()); dbQueryInputData.addSingleDBQueryInputData(singleQueryInputData); final ICommunicationProtocol newDbQueryInputData = execution.startInputData(singleQueryInputData); newDbQueryInputData.setRequestId(singleResponseData.getRequestId()); }/*from w w w. jav a2 s. co m*/ execution.endExecution(responseData); final IProcessTransition lastTransition = execution.getLastTransition(); assertNotNull(lastTransition); final IStatus currentStatus = lastTransition.getCurrentStatus(); assertNotNull(currentStatus); assertEquals(PersistentStatus.DONE.name(), currentStatus.getName()); final Set<ICommunicationProtocol> inputDataSet = execution.getCommunicationProtocols(); for (final ICommunicationProtocol iInputData : inputDataSet) { final String requestId = iInputData.getRequestId(); final Collection<ISingleResponseData> responses = responseData.getResponse(requestId); assertEquals("Unexpected Reponse", 1, responses.size()); ISingleResponseData response = (ISingleResponseData) CollectionUtils.get(responses, 0); assertEquals(response.getResponseId(), iInputData.getResponseId()); assertEquals(response.getReturnCode(), iInputData.getReturnCode()); assertEquals(response.getReturnText(), iInputData.getReturnText()); } for (final ICommunicationProtocol iquelleInputData : sourceInputDataList) { // refresh final CommunicationProtocol quelleInputData = communicationProtocolRepository .findOne(iquelleInputData.getId()); // prfen PhaseConnection final IPhaseConnection quellePhaseConnection = quelleInputData.getNextPhaseConnection(); final ICommunicationProtocol targetInputData = quellePhaseConnection.getTargetCommunicationProtocol(); assertNotNull(targetInputData); final IStatus quellePhaseConnectionStatus = quellePhaseConnection.getStatus(); assertNotNull(quellePhaseConnectionStatus); assertEquals(PersistentStatus.DONE.name(), quellePhaseConnectionStatus.getName()); } }
From source file:nl.strohalm.cyclos.controls.access.ListConnectedUsersForm.java
public String getNature() { final Object natures = getQuery(NATURES_KEY); try {// w ww . j a v a 2 s.com return "" + CollectionUtils.get(natures, 0); } catch (final Exception e) { return null; } }
From source file:nl.ucan.navigate.NestedPath.java
private NestedPath() { this.propertyInstance = new PropertyInstance() { public Object indexed(Object bean, String property, int index, Object value) { log.info("created indexed property " + property + " at " + index + " of bean " + bean + " and will be set to " + value); return value; }// w ww. java 2 s. c o m public Object simple(Object bean, String property, Object value) { log.info("created simple property " + property + " of bean " + bean + " and will be set to " + value); return value; } }; this.propertyValue = new PropertyValue() { public Object indexed(Object bean, String property, int index, Object value) { log.info("value of indexed property " + property + " at " + index + " of bean " + bean + " will be set to " + value); return value; } public Object mapped(Object bean, String property, Object key, Object value) { log.info("value of mapped property " + property + " at " + key + " of bean " + bean + " will be set to " + value); return value; } public Object simple(Object bean, String property, Object value) { log.info("value of simple property " + property + " of bean " + bean + " will be set to " + value); return value; } public Object valueOf(Class clasz, String property, String value) { log.info("value of valueOf " + property + " will be set to " + value); return value; } }; this.indexPointer = new IndexPointer() { public int size(Object bean) { return CollectionUtils.size(bean); } public Object get(Object bean, int idx) { return CollectionUtils.get(bean, idx); } public int firstIndexOf(Object bean, String undeterminedIndex) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, InstantiationException, IntrospectionException { this.setUndeterminedIndex(undeterminedIndex); for (int idx = 0; idx < size(bean); idx++) { Object object = get(bean, idx); if (object != null) { if (evaluate(object, this.getUndeterminedIndex())) return idx; } } return -1; } public void setIndexAsProperty(Object bean, String undeterminedIndex) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, InstantiationException, IntrospectionException { this.setUndeterminedIndex(undeterminedIndex); Object value = propertyValue.simple(bean, this.getProperty(), this.getValue()); pub.setProperty(bean, this.getProperty(), value); } private String undeterminedIndex; private void setUndeterminedIndex(String undeterminedIndex) { this.undeterminedIndex = undeterminedIndex; } private String getUndeterminedIndex() { return this.undeterminedIndex; } private boolean evaluate(Object bean, String undeterminedIndex) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, InstantiationException, IntrospectionException { this.setUndeterminedIndex(undeterminedIndex); String property = getProperty(); String valueOfIndex = getValue(); Object valueOfBean = pub.getProperty(bean, property); return ObjectUtils.equals(valueOfIndex, valueOfBean); } private String getProperty() { Map.Entry<String, String> entry = getNamedIndex(this.getUndeterminedIndex()); return entry.getKey(); } private String getValue() { Map.Entry<String, String> entry = getNamedIndex(this.getUndeterminedIndex()); return entry.getValue(); } private Map.Entry<String, String> getNamedIndex(String value) { final String SEP = "="; Map<String, String> keyValuePair = new HashMap<String, String>(); if (StringUtils.indexOf(value, SEP) == -1) return null; keyValuePair.put(StringUtils.substringBefore(value, SEP), StringUtils.substringAfter(value, SEP)); return keyValuePair.entrySet().iterator().next(); } }; this.pub = BeanUtilsBean.getInstance().getPropertyUtils(); this.pub.setResolver(new ResolverImpl()); }
From source file:org.apache.stanbol.client.StanbolClientTest.java
@Test public void testEntityHub() throws IOException, StanbolServiceException, StanbolClientException { final EntityHub client = factory.createEntityHubClient(); final String resourceId = "http://dbpedia.org/resource/Doctor_Who"; final String parisId = "http://dbpedia.org/resource/Paris"; final String ldPathProgram = "@prefix find:<http://stanbol.apache.org/ontology/entityhub/find/>; find:labels = rdfs:label[@en] :: xsd:string; find:comment = rdfs:comment[@en] :: xsd:string; find:categories = dc:subject :: xsd:anyURI; find:mainType = rdf:type :: xsd:anyURI;"; // Create the entity try (final InputStream entityContentStream = this.getClass().getClassLoader() .getResourceAsStream(TEST_RDF_FILE)) { String id = client.create(entityContentStream, resourceId, true); Assert.assertNotNull(id);//from w w w . j a v a 2 s . c o m Assert.assertNotEquals(id.toString().indexOf(resourceId), -1); } // Get the entity Entity entity = client.get(resourceId); Assert.assertNotNull(entity); Assert.assertEquals("Doctor Who", entity.getLabels("en").iterator().next()); Assert.assertEquals(resourceId, entity.getUri()); // Test Entity Model Assert.assertEquals("entityhub", entity.getReferencedSite()); Assert.assertEquals("http://dbpedia.org/resource/Category:BBC_television_programmes", entity.getCategories().iterator().next()); Assert.assertEquals("http://schema.org/CreativeWork", entity.getTypes().iterator().next()); Assert.assertNotNull(entity.getComments("en").iterator().next()); Collection<String> labels = entity.getLabels("en"); Assert.assertEquals(1, labels.size()); Assert.assertEquals("Doctor Who", entity.getLabels("en").iterator().next()); Assert.assertEquals("777", entity.getPropertyValues("http://dbpedia.org/property/", "numEpisodes").iterator().next()); // Remove the entity boolean removed = client.delete(resourceId); Assert.assertTrue(removed); // Try to get the entity Assert.assertNull(client.get(resourceId)); // Test Get Entity Site Entity paris = client.get("dbpedia", parisId); Assert.assertNotNull(paris); Assert.assertEquals(parisId, paris.getUri()); Assert.assertEquals("dbpedia", paris.getReferencedSite()); Assert.assertEquals("http://dbpedia.org/resource/Category:3rd-century_BC_establishments", CollectionUtils.get(paris.getCategories(), 1)); Assert.assertEquals("2211297", paris.getPropertyValues("http://dbpedia.org/ontology/", "populationTotal").iterator().next()); // Test Lookup paris = client.lookup(parisId, true); entity = client.get(paris.getUri()); Assert.assertNotNull(entity); Assert.assertTrue(client.delete(paris.getUri())); // Test Search LDPathProgram program = new LDPathProgram(ldPathProgram); Collection<Entity> entities = client.search("Paris*", null, "en", program, 10, 0); Assert.assertTrue(entities.isEmpty()); entities = client.search("dbpedia", "Paris*", null, "en", program, 10, 0); Assert.assertFalse(entities.isEmpty()); List<Entity> eList = Lists.newArrayList(entities); assertEquals("Civil parishes in England", eList.get(2).getPropertyValues("http://stanbol.apache.org/ontology/entityhub/find/", "labels") .iterator().next()); // Test ldpath program = new LDPathProgram(); program.addNamespace("find", "http://stanbol.apache.org/ontology/entityhub/find/"); program.addFieldDefinition("find:categories", "dc:subject :: xsd:anyURI;"); Model model = client.ldpath("dbpedia", parisId, program); String category = model .listObjectsOfProperty(model.getResource(parisId), model.createProperty(program.getNamespace("find"), "categories")) .next().asResource().getURI(); Assert.assertEquals("http://dbpedia.org/resource/Category:Paris", category); }
From source file:org.apache.stanbol.client.test.StanbolClientTest.java
@Test public void testEntityHub() throws Exception { final EntityHub client = factory.createEntityHubClient(); final String resourceId = "http://dbpedia.org/resource/Doctor_Who"; final String parisId = "http://dbpedia.org/resource/Paris"; final String ldPathProgram = "@prefix find:<http://stanbol.apache.org/ontology/entityhub/find/>; find:labels = rdfs:label[@en] :: xsd:string; find:comment = rdfs:comment[@en] :: xsd:string; find:categories = dc:subject :: xsd:anyURI; find:mainType = rdf:type :: xsd:anyURI;"; // Create the entity String id = client.create(this.getClass().getClassLoader().getResourceAsStream(TEST_RDF_FILE), resourceId, true);/* w w w .j a v a 2 s.c o m*/ Assert.assertNotNull(id); Assert.assertNotEquals(id.toString().indexOf(resourceId), -1); // Get the entity Entity entity = client.get(resourceId); Assert.assertNotNull(entity); Assert.assertEquals(entity.getLabels("en").iterator().next(), "Doctor Who"); Assert.assertEquals(entity.getUri(), resourceId); // Test Entity Model Assert.assertEquals(entity.getReferencedSite(), "entityhub"); Assert.assertEquals(entity.getCategories().iterator().next(), "http://dbpedia.org/resource/Category:BBC_television_programmes"); Assert.assertEquals(entity.getTypes().iterator().next(), "http://schema.org/CreativeWork"); Assert.assertNotNull(entity.getComments("en").iterator().next()); Collection<String> labels = entity.getLabels("en"); Assert.assertEquals(labels.size(), 1); Assert.assertEquals(entity.getLabels("en").iterator().next(), "Doctor Who"); Assert.assertEquals( entity.getPropertyValues("http://dbpedia.org/property/", "numEpisodes").iterator().next(), "777"); // Remove the entity boolean removed = client.delete(resourceId); Assert.assertTrue(removed); // Try to get the entity Assert.assertNull(client.get(resourceId)); // Test Get Entity Site Entity paris = client.get("dbpedia", parisId); Assert.assertNotNull(paris); Assert.assertEquals(paris.getUri(), parisId); Assert.assertEquals(paris.getReferencedSite(), "dbpedia"); Assert.assertEquals(CollectionUtils.get(paris.getCategories(), 1), "http://dbpedia.org/resource/Category:3rd-century_BC_establishments"); Assert.assertEquals( paris.getPropertyValues("http://dbpedia.org/ontology/", "populationTotal").iterator().next(), "2211297"); // Test Lookup paris = client.lookup(parisId, true); entity = client.get(paris.getUri()); Assert.assertNotNull(entity); Assert.assertTrue(client.delete(paris.getUri())); // Test Search LDPathProgram program = new LDPathProgram(ldPathProgram); Collection<Entity> entities = client.search("Paris*", null, "en", program, 10, 0); Assert.assertTrue(entities.isEmpty()); entities = client.search("dbpedia", "Paris*", null, "en", program, 10, 0); Assert.assertFalse(entities.isEmpty()); List<Entity> eList = Lists.newArrayList(entities); assertEquals(eList.get(2).getPropertyValues("http://stanbol.apache.org/ontology/entityhub/find/", "labels") .iterator().next(), "Civil parishes in England"); // Test ldpath program = new LDPathProgram(); program.addNamespace("find", "http://stanbol.apache.org/ontology/entityhub/find/"); program.addFieldDefinition("find:categories", "dc:subject :: xsd:anyURI;"); Model model = client.ldpath("dbpedia", parisId, program); String category = model .listObjectsOfProperty(model.getResource(parisId), model.createProperty(program.getNamespace("find"), "categories")) .next().asResource().getURI(); Assert.assertEquals(category, "http://dbpedia.org/resource/Category:Paris"); }
From source file:org.gradle.nativeplatform.toolchain.internal.msvcpp.VisualCppNativeCompiler.java
@Override protected List<String> getPCHArgs(T spec) { List<String> pchArgs = new ArrayList<String>(); if (CollectionUtils.isNotEmpty(spec.getPreCompiledHeaders()) && spec.getPreCompiledHeaderObjectFile() != null) { String lastHeader = (String) CollectionUtils.get(spec.getPreCompiledHeaders(), spec.getPreCompiledHeaders().size() - 1); if (lastHeader.startsWith("<")) { lastHeader = lastHeader.substring(1, lastHeader.length() - 1); }//from w ww.j a v a2s. co m pchArgs.add("/Yu".concat(lastHeader)); pchArgs.add("/Fp".concat(spec.getPreCompiledHeaderObjectFile().getAbsolutePath())); } return pchArgs; }
From source file:org.jbuiltDemo.managed.view.Xhtml2Jbuilt.java
public void convertNode(ElementNode node, StringBuffer code, String tabs) { if (node.textOnly) { if (node.text != null && !"".equals(node.text.trim())) { String[] lines = node.text.trim().split("\n"); if (lines.length == 1) { code.append(tabs).append("text( \"").append(node.text.trim()).append("\")/*TM*/\n") .append(tabs);//.append(")/*TN*/,\n");//.append(tabs); } else { code.append("\n").append(tabs).append("text( \"\n"); for (String line : lines) { code.append(tabs).append("\t\"").append(line).append("\",\n"); }/*from ww w . j av a2 s . c om*/ code.append(tabs).append("\")/*TO*/\n").append(tabs).append(")/*TP*/,\n").append(tabs); } } else if (node.comments != null) { code.append("\n"); for (String line : node.comments.split("\n")) { code.append(tabs).append("//").append(line).append("\n"); } } else if (node.text.contains("\n")) { code.append("\n"); } return; } String ns = defaultMappings.containsKey(node.mapping) ? defaultMappings.get(node.mapping) : info.mappings.get(node.mapping); if (ns == null || "".equals(ns)) { ns = "xh"; } boolean t = false; //node.areChildrenOnlyText(); String txt = node.getChildrenAsText(); if (txt.length() > 40 || txt.length() == 0) { t = false; } code.append(tabs)./*append(ns).append(".").*/append(node.name); if (node.attributes.size() > 0 || t || node.children.size() == 0) { code.append(" (/*T*/\n").append(tabs).append("\t"); } if (t) { code.append("\"").append(txt.replaceAll("\"", "\\\\\"")).append("\""); } int atts = 0; Set<String> nodeAttributeSet = node.attributes.keySet(); for (String an : nodeAttributeSet) { if ("".equals(an)) { continue; } if (atts > 0 || t) { code.append(""); // was ,/*C*/ } // if (an.matches(".*[^a-zA-Z0-9]+.*")) code.append("\"").append(an).append("\""); /*else*/ code.append(an); code.append("(/*S*/").append("\""); // if this is the last one, don't add a comma if (CollectionUtils.get(nodeAttributeSet, nodeAttributeSet.size() - 1) == an) { code.append(node.attributes.get(an)).append("\")/*P*/\n\t\t"); } else { // if this is last node wrap an extra comma and parenthese code.append(node.attributes.get(an)).append("\"),/*P2*/\n\t").append(tabs); } atts++; } if (node.children.size() > 0 && !t) { String ntabs = tabs + "\t"; if (node.attributes.size() > 0 || node.children.size() == 0) { code.append("");// code.append("(),\n"); // was )/*Y*/ } code.append(" (/*Z*/\n"); this.compactTextChildren(node); for (ElementNode child : node.children) { this.convertNode(child, code, ntabs); } if (node.attributes.size() > 0 || node.children.size() == 0) { code.append(tabs).append(")/*L no CH*/\n"); } else { code.append(tabs).append("),/*L*/\n"); } } else if (node.attributes.size() > 0 || t || node.children.size() == 0) { code.append(")/*Atts no Ch and text*/\n").append("\t");//.append("),/*N*/\n"); } }
From source file:org.keycloak.testsuite.console.authentication.FlowsTest.java
private AuthenticationFlowRepresentation getLastFlowFromREST() { List<AuthenticationFlowRepresentation> allFlows = testRealmResource().flows().getFlows(); return (AuthenticationFlowRepresentation) CollectionUtils.get(allFlows, (allFlows.size() - 1)); }