List of usage examples for org.apache.commons.lang3.tuple ImmutablePair of
public static <L, R> ImmutablePair<L, R> of(final L left, final R right)
Obtains an immutable pair of from two objects inferring the generic types.
This factory allows the pair to be created using inference to obtain the generic types.
From source file:cc.kave.commons.pointsto.analysis.inclusion.graph.ConstraintGraphBuilder.java
private void registerVolatileEntity(IIndexAccessReference indexAccessRef) { DistinctIndexAccessReference distRef = (DistinctIndexAccessReference) referenceResolver .apply(indexAccessRef);/*from w w w . j av a 2 s . c o m*/ DistinctReference baseDistRef = distRef.getBaseReference(); SetVariable setVar = getVariable(baseDistRef); Pair<DistinctReference, Class<? extends ISSTNode>> key = ImmutablePair.of(baseDistRef, IIndexAccessReference.class); if (!volatileEntities.containsKey(key)) { SetVariable content = createTemporaryVariable(); volatileEntities.put(key, content); writeArrayRaw(setVar, content); readArrayRaw(content, setVar); } }
From source file:com.wrmsr.wava.TestWhatever.java
public static void doFunction(com.wrmsr.wava.core.unit.Function function_) throws Exception { System.out.println(function_.getName().get()); com.wrmsr.wava.core.unit.Function function = new StandardFunctionProcessor().processFunction(function_); BasicSet basics;/*from ww w . j a v a2 s . c om*/ { Node root = function.getBody(); Map<Name, Node> namedNodes = Analyses.getNamedNodes(root); Map<Node, Name> namedNodeNames = namedNodes.entrySet().stream() .collect(toIdentityMap(e -> e.getValue(), e -> e.getKey())); List<Node> nodes = Analyses.linearize(root); Map<Node, Integer> nodeIndices = indexIdentityMap(nodes); Map<Node, Name> nodeNames = nodes.stream() .map(node -> ImmutablePair.of(node, namedNodeNames.containsKey(node) ? namedNodeNames.get(node) : Name.of("node" + nodeIndices.get(node)))) .collect(toIdentityMap(Map.Entry::getKey, Map.Entry::getValue)); ValueTypeAnalysis vta = ValueTypeAnalysis.analyze(root, false); ControlFlowGraph cfg = ControlFlowGraph.analyzeShallow(root, namedNodes, vta); basics = BasicSet.build( Basics.buildBasics(cfg, vta, nodeNames, nodeIndices).values().stream().map(Basics::cleanBasic)); } // showBasics(basics, true); // { // Node root = worst(basics, findBasicLoops(basics, generateBasicDominatorTree(basics))); // JDeclaration jdecl = jcompileFunction(new com.wrmsr.wava.core.unit.Function(function.getName(), function.getResult(), function.getArgCount(), function.getLocals(), root)).get(0); // CodeBlock.Builder code = CodeBlock.builder(); // new JRenderer(code).renderDeclaration(jdecl); // System.out.println(JRenderer.renderWithIndent(code.build(), " ")); // } while (true) { int size = basics.size(); System.out.println(size); basics = transformBasics(SimpleMatching::mergeUnconditionalBasic, basics); basics = transformBasics(SimpleMatching::mergeEmptyBasic, basics); basics = transformBasics(BooleanMatching::shrinkIf, basics); basics = transformBasics(BooleanMatching::shrinkIfElse, basics); basics = transformBasics(bind(LoopMatching::shrinkSelfLoops, BasicLoopInfo.build(basics, BasicDominatorInfo.build(basics)))::apply, basics); basics = transformBasics(bind(TestWhatever::shrinkSimpleLoop, BasicLoopInfo.build(basics, BasicDominatorInfo.build(basics)))::apply, basics); basics = transformBasics(TestWhatever::collapseIfOr, basics); if (basics.size() == size) { break; } } showBasics(function.getName(), basics, true); // getLoopContents(basics, Name.of("node155")); // Map<Name, Name> loopParents = new HashMap<>(); // { // Stack<Name> rootStack = new Stack<>(); // rootStack.push(null); // Set<Name> set = new HashSet<>(loops); // while (!rootStack.isEmpty()) { // Name parent = rootStack.pop(); // if (parent != null) { // set.remove(parent); // } // Set<Name> rootLoops = set.stream().filter(loop -> !set.stream().anyMatch(otherLoop -> !otherLoop.equals(loop) && loopContents.get(otherLoop).contains(loop))).collect(toImmutableSet()); // for (Name rootLoop : rootLoops) { // loopParents.put(rootLoop, parent); // rootStack.push(rootLoop); // } // } // checkState(set.isEmpty()); // } // if (basics.size() > 3 && basics.size() < 8) { // showBasics(function.getName(), basics, true); // } // cfgStackify(basics); // DirectedGraph<Integer, DefaultEdge> graph = new DefaultDirectedGraph<>(DefaultEdge.class); // nodeIndices.values().forEach(graph::addVertex); // cfg.getEdges().forEach(e -> graph.addEdge(nodeIndices.get(e.getInput()), nodeIndices.get(e.getOutput()))); // DominatorTree<Integer, DefaultEdge> dt = new DominatorTree<>(graph, 0); }
From source file:com.wrmsr.neurosis.util.Configs.java
protected static Map<Sigil, String> flattenList(List<?> list) { return IntStream.range(0, list.size()).boxed() .flatMap(i -> flattenValues(list.get(i)).entrySet().stream() .map(e -> ImmutablePair.of(new ListItemSigil(i, e.getKey()), e.getValue()))) .collect(ImmutableCollectors.toImmutableMap(e -> e.getKey(), e -> e.getValue())); }
From source file:com.offbynull.portmapper.upnpigd.UpnpIgdController.java
/** * Add a port mapping.//from w w w . j av a2 s.co m * @param externalPort external port * @param internalPort internal port * @param portType port type * @param duration mapping duration (0 = indefinite, may or may not be supported by router) * @return port mapping information for the new mapping * @throws NullPointerException if portType is {@code null} * @throws IllegalArgumentException if {@code externalPort} isn't between {@code 0} to {@code 65535}, or if {@code externalPort} * isn't between the external port range specified by the service (if one was specified), or if {@code internalPort} isn't between * {@code 1} to {@code 65535}, or if {@code duration} isn't between the duration range specified by the service (if one was specified) * @throws ResponseException if the router responds with an error, or if {@code duration} is negative */ public PortMappingInfo addPortMapping(int externalPort, int internalPort, PortType portType, long duration) { // CHECKSTYLE:ON Validate.inclusiveBetween(0, 65535, externalPort); // 0 = wildcard, any unassigned port? may not be supported according to docs Validate.inclusiveBetween(1, 65535, internalPort); Validate.notNull(portType); Validate.inclusiveBetween(0L, Long.MAX_VALUE, duration); if (externalPortRange != null) { Validate.inclusiveBetween((long) externalPortRange.getMinimum(), (long) externalPortRange.getMaximum(), (long) externalPort); } if (leaseDurationRange != null) { if (duration == 0L) { Validate.isTrue(leaseDurationRange.getMinimum() == 0, "Infinite duration not allowed"); } duration = Math.max(leaseDurationRange.getMinimum(), duration); duration = Math.min(leaseDurationRange.getMaximum(), duration); } performRequest("AddPortMapping", ImmutablePair.of("NewRemoteHost", ""), ImmutablePair.of("NewExternalPort", "" + externalPort), ImmutablePair.of("NewProtocol", portType.name()), ImmutablePair.of("NewInternalPort", "" + internalPort), ImmutablePair.of("NewInternalClient", selfAddress.getHostAddress()), ImmutablePair.of("NewEnabled", "1"), ImmutablePair.of("NewPortMappingDescription", ""), ImmutablePair.of("NewLeaseDuration", "" + duration)); PortMappingInfo info = getMappingDetails(externalPort, portType); activePortsLock.lock(); try { activePorts.put(externalPort, info); } finally { activePortsLock.unlock(); } return info; }
From source file:edu.ksu.cis.santos.mdcf.dml.symbol.SymbolTable.java
/** * Retrieves an immutable {@link Map} of {@link Member#name} to * {@link Invariant} with its declaring {@link Feature} that contains all * declared and closest (least) inherited members of the provided feature. * //from www.j a va 2 s . co m * @param featureName * The fully-qualified name ({@link Feature#name}) of the * {@link Feature} whose members to be retrieved. * @return an immutable {@link Map}. */ public Map<String, Pair<Feature, Member>> allMemberMap(final String featureName) { final Map<String, Map<String, Pair<Feature, Member>>> map = featureMemberMap(); Map<String, Pair<Feature, Member>> result = map.get(featureName); if (result == null) { final TreeMap<String, Pair<Feature, Member>> b = new TreeMap<>(); final Feature feature = feature(featureName); for (final NamedType nt : feature.supers) { b.putAll(allMemberMap(nt.name)); } for (final Member m : feature.members) { b.put(m.name, ImmutablePair.of(feature, m)); } result = Collections.unmodifiableMap(b); map.put(featureName, result); } return result; }
From source file:cc.kave.commons.pointsto.analysis.inclusion.ConstraintGenerationVisitorContext.java
public void enterLambda(ILambdaExpression lambdaExpr) { IAssignableReference destRef = getDestinationForExpr(lambdaExpr); SetVariable tempDest = (destRef != null) ? builder.createTemporaryVariable() : ConstructedTerm.BOTTOM; SetVariable returnVar = builder.storeFunction(tempDest, lambdaExpr); lambdaStack.addFirst(ImmutablePair.of(lambdaExpr, returnVar)); if (destRef != null) { assign(destRef, tempDest);/*from w w w . jav a 2 s . co m*/ } }
From source file:com.wrmsr.neurosis.util.Configs.java
protected static Map<Sigil, String> flattenMap(Map<?, ?> map) { return map.entrySet().stream().flatMap(e -> { String key = Objects.toString(e.getKey()); return flattenValues(e.getValue()).entrySet().stream() .map(e2 -> ImmutablePair.of(new MapEntrySigil(key, e2.getKey()), e2.getValue())); }).collect(ImmutableCollectors.toImmutableMap(e -> e.getKey(), e -> e.getValue())); }
From source file:cc.kave.commons.pointsto.analysis.inclusion.graph.ConstraintGraphBuilder.java
private void registerVolatileEntities(ILambdaExpression lambdaExpr) { for (IParameterName parameter : lambdaExpr.getName().getParameters()) { if (!parameter.isOutput()) { DistinctReference distRef = new DistinctLambdaParameterReference(parameter, lambdaExpr); Pair<DistinctReference, Class<? extends ISSTNode>> key = ImmutablePair.of(distRef, ILambdaExpression.class); if (!volatileEntities.containsKey(key)) { volatileEntities.put(key, getVariable(distRef)); }// w w w .ja v a 2s . c om } } }
From source file:com.shieldsbetter.sbomg.ViewModelGenerator.java
private static void outfitModelWithList(ModelClass dest, String contextName, Set<String> listOptions, Object elDesc) {/*from www. jav a 2 s . co m*/ boolean immutableFlag = listOptions.contains("immutable"); boolean replaceableFlag = listOptions.contains("replaceable"); ListElementTypeData elTypeData = outfitModelWithListElementType(dest, contextName, elDesc); String elTypeRaw = elTypeData.getRawTypeName(); TypeName elType = ClassName.get("", elTypeRaw); String slotTypeRaw = contextName + "Key"; TypeName slotType = ClassName.get("", slotTypeRaw); TypeSpec.Builder keyType = TypeSpec.classBuilder(slotTypeRaw) .addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL) .addMethod(MethodSpec.constructorBuilder().addModifiers(Modifier.PRIVATE) .addParameter(elType, "value").addStatement("myValue = value").build()) .addMethod(MethodSpec.methodBuilder("getValue").addModifiers(Modifier.PRIVATE).returns(elType) .addStatement("return myValue").build()); if (elTypeData.isFinal() || immutableFlag) { keyType.addField(elType, "myValue", Modifier.PRIVATE, Modifier.FINAL); } else { keyType.addField(elType, "myValue", Modifier.PRIVATE); keyType.addMethod(MethodSpec.methodBuilder("setValue").addModifiers(Modifier.PRIVATE) .addParameter(elType, "value").addStatement("myValue = value").build()); } dest.addType(keyType.build()); FieldSpec.Builder field = FieldSpec .builder(ParameterizedTypeName.get(ClassName.get("java.util", "List"), slotType), "my" + contextName + "List", Modifier.PRIVATE) .initializer("new $T<>()", ClassName.get("java.util", "LinkedList")); if (!replaceableFlag) { field.addModifiers(Modifier.FINAL); } dest.addField(field.build()); dest.addRootMethod("get" + contextName + "Element", elType, ImmutableList.of(ImmutablePair.of("index", TypeName.INT)), CodeBlock.builder().addStatement("return my$LList.get(index).getValue()", contextName).build()); dest.addRootMethod("get" + contextName + "Element", elType, ImmutableList.of(ImmutablePair.of("key", slotType)), CodeBlock.builder().addStatement("int index = my$LList.indexOf(key)", contextName) .addStatement("return my$LList.get(index).getValue()", contextName).build()); // TODO: Fix this. The generated field contians keys not elements. /* dest.addRootMethod("contains" + contextName + "Element", TypeName.BOOLEAN, ImmutableList.of( ImmutablePair.of("element", elType)), renderCodeBlock(LIST_CONTAINS_VALUE_METHOD_TEMPL, "fieldName", contextName, "valueParam", "element")); */ if (contextName.isEmpty()) { String parentInterfaceName = elTypeRaw; if (elTypeData.isInnerClass()) { parentInterfaceName = dest.getName() + "." + parentInterfaceName; } dest.addImplements(ParameterizedTypeName.get(ClassName.get("", "Iterable"), ClassName.get("", parentInterfaceName))); dest.addOverriddenRootMethod("iterator", ParameterizedTypeName.get(ClassName.get(Iterator.class), elType), ImmutableList.of(), renderCodeBlock(LIST_ITERATOR_TEMPL, "elementType", elTypeRaw, "contextName", contextName, "baseIterable", "my" + contextName + "List")); } else { // Gross workaround here. JavaPoet doesn't provide any way to // include a random non-static import. So we render out a template // that happens to include an unresolved $T, then replace it with // the type we want to import. dest.addRootMethod(CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, contextName), ParameterizedTypeName.get(ClassName.get("", "Iterable"), elType), ImmutableList.of(), CodeBlock.of( renderTemplate(RETURN_LIST_METHOD_TEMPL, "elementType", elTypeRaw, "iteratorCode", renderTemplate(LIST_ITERATOR_TEMPL, "elementType", elTypeRaw, "contextName", contextName, "baseIterable", "my" + contextName + "List")), ParameterizedTypeName.get(ClassName.get(Iterator.class), elType))); } if (immutableFlag) { String elementParam = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, contextName + "Elements"); dest.addInitializationParameter(elementParam, ParameterizedTypeName.get(ClassName.get("", "Iterable"), elType)); CodeBlock.Builder init = CodeBlock.builder().beginControlFlow("for ($T e : $L)", elType, elementParam) .addStatement("$LKey k = new $LKey(e);", contextName, contextName) .addStatement("my$LList.add(k)", contextName); if (!elTypeData.isLeaf()) { init.addStatement("subscribeIfNotNull(k)"); } init.endControlFlow(); dest.addInitializationCode(init.build()); } if (replaceableFlag) { dest.addListenerEvent(contextName + "Replaced", ImmutableList.of(ImmutablePair.of("newValue", ParameterizedTypeName.get(ClassName.get("java.util", "List"), elType)))); dest.addRootMethod("replace" + contextName, TypeName.VOID, ImmutableList.of(ImmutablePair.of("elements", ParameterizedTypeName.get(ClassName.get("", "Iterable"), elType))), renderCodeBlock(REPLACE_LIST_METHOD_TEMPL, "contextName", contextName, "elementType", elTypeRaw, "parentType", dest.getName(), "elementsParam", "elements", "leafFlag", elTypeData.isLeaf())); dest.addBuildCode(CodeBlock.builder().beginControlFlow("") .addStatement("List<$T> valueList = new $T<>()", elType, LinkedList.class) .beginControlFlow("for ($T s : my$LList)", slotType, contextName) .addStatement("valueList.add(s.getValue())").endControlFlow() .addStatement("l.on$LReplaced(this, " + "$T.unmodifiableList(valueList))", contextName, Collections.class) .endControlFlow().build()); } if (!elTypeData.isLeaf()) { dest.addField(FieldSpec .builder( ParameterizedTypeName.get(ClassName.get("java.util", "Map"), slotType, ClassName.get("", elTypeRaw + ".Subscription")), "my" + contextName + "Subscriptions", Modifier.PRIVATE, Modifier.FINAL) .initializer("new $T<>()", ClassName.get("java.util", "HashMap")).build()); dest.addListenerEvent(contextName + "Updated", ImmutableList.of(ImmutablePair.of("updatedElement", elType), ImmutablePair.of("index", TypeName.INT), ImmutablePair.of("key", slotType), ImmutablePair.of("event", ClassName.get("", elTypeRaw + ".Event")))); dest.addRootMethod(MethodSpec.methodBuilder("subscribeIfNotNull").returns(TypeName.VOID) .addModifiers(Modifier.PRIVATE).addParameter(slotType, "key", Modifier.FINAL) .addCode(renderCodeBlock(SUBSCRIBE_IF_NOT_NULL_TEMPL, "keyParam", "key", "fieldName", contextName, "fieldType", elTypeRaw, "parentType", dest.getName())) .build()); } if (!immutableFlag) { if (!replaceableFlag) { dest.addBuildCode(CodeBlock.builder().beginControlFlow("").addStatement("int i = 0") .beginControlFlow("for ($T s : my$LList)", slotType, contextName) .addStatement("l.on$LAdded(this, s.getValue(), i, s)", contextName).addStatement("i++") .endControlFlow().endControlFlow().build()); } dest.addListenerEvent(contextName + "Added", ImmutableList.of(ImmutablePair.of("addedElement", elType), ImmutablePair.of("index", TypeName.INT), ImmutablePair.of("key", slotType))); dest.addRootMethod("add" + contextName, slotType, ImmutableList.of(ImmutablePair.of("newElement", elType)), renderCodeBlock(LIST_ADD_METHOD_TEMPL, "valueParam", "newElement", "fieldName", contextName, "fieldType", elTypeRaw, "leafFlag", elTypeData.isLeaf(), "parentType", dest.getName())); dest.addListenerEvent(contextName + "Removed", ImmutableList.of(ImmutablePair.of("removedElement", elType), ImmutablePair.of("index", TypeName.INT), ImmutablePair.of("key", slotType))); dest.addRootMethod("remove" + contextName, TypeName.VOID, ImmutableList.of(ImmutablePair.of("index", TypeName.INT)), renderCodeBlock( LIST_REMOVE_METHOD_BY_INDEX_TEMPL, "indexParam", "index", "fieldName", contextName)); dest.addRootMethod("remove" + contextName, TypeName.VOID, ImmutableList.of(ImmutablePair.of("key", slotType)), renderCodeBlock(LIST_REMOVE_METHOD_BY_KEY_TEMPL, "keyParam", "key", "fieldName", contextName)); dest.addRootMethod(MethodSpec.methodBuilder("remove" + contextName).addModifiers(Modifier.PRIVATE) .returns(TypeName.VOID).addParameter(TypeName.INT, "index", Modifier.FINAL) .addParameter(slotType, "key", Modifier.FINAL) .addCode(renderCodeBlock(LIST_REMOVE_METHOD_CORE_TEMPL, "fieldName", contextName, "keyParam", "key", "indexParam", "index", "leafFlag", elTypeData.isLeaf(), "parentType", dest.getName())) .build()); dest.addRootMethod("clear" + contextName, TypeName.VOID, ImmutableList.<ImmutablePair<String, TypeName>>of(), renderCodeBlock(LIST_CLEAR_METHOD_TEMPL, "fieldName", contextName)); if (!elTypeData.isFinal()) { dest.addListenerEvent(contextName + "Set", ImmutableList.of(ImmutablePair.of("oldValue", elType), ImmutablePair.of("newValue", elType), ImmutablePair.of("index", TypeName.INT), ImmutablePair.of("key", slotType))); dest.addRootMethod("set" + contextName, TypeName.VOID, ImmutableList.of(ImmutablePair.of("index", TypeName.INT), ImmutablePair.of("newValue", elType)), renderCodeBlock(LIST_SET_METHOD_BY_INDEX_TEMPL, "fieldName", contextName, "indexParam", "index", "valueParam", "newValue")); dest.addRootMethod("set" + contextName, TypeName.VOID, ImmutableList.of(ImmutablePair.of("key", slotType), ImmutablePair.of("newValue", elType)), renderCodeBlock(LIST_SET_METHOD_BY_KEY_TEMPL, "keyParam", "key", "fieldName", contextName, "valueParam", "newValue")); dest.addRootMethod(MethodSpec.methodBuilder("set" + contextName).returns(TypeName.VOID) .addModifiers(Modifier.PRIVATE).addParameter(TypeName.INT, "index", Modifier.FINAL) .addParameter(slotType, "key", Modifier.FINAL).addParameter(elType, "value", Modifier.FINAL) .addCode(renderCodeBlock(LIST_SET_METHOD_CORE_TEMPL, "keyParam", "key", "indexParam", "index", "valueParam", "value", "leafFlag", elTypeData.isLeaf(), "fieldName", contextName, "fieldType", elTypeRaw, "parentType", dest.getName())) .build()); } } }
From source file:com.offbynull.portmapper.upnpigd.UpnpIgdController.java
/** * Delete a port mapping.//from w w w. jav a 2 s .c o m * @param externalPort external port * @param portType port type * @throws NullPointerException if portType is {@code null} * @throws IllegalArgumentException if {@code externalPort} isn't between {@code 0} to {@code 65535}, or if the {@code externalPort} * isn't between the external port range specified by the service (if one was specified) * @throws ResponseException if the router responds with an error */ public void deletePortMapping(int externalPort, PortType portType) { // CHECKSTYLE:ON Validate.inclusiveBetween(1, 65535, externalPort); Validate.notNull(portType); if (externalPortRange != null) { Validate.inclusiveBetween((long) externalPortRange.getMinimum(), (long) externalPortRange.getMaximum(), (long) externalPort); } /*PortMappingInfo info = */getMappingDetails(externalPort, portType); performRequest("DeletePortMapping", ImmutablePair.of("NewRemoteHost", ""), ImmutablePair.of("NewExternalPort", "" + externalPort), ImmutablePair.of("NewProtocol", portType.name())); activePortsLock.lock(); try { activePorts.remove(externalPort); } finally { activePortsLock.unlock(); } }