@Test public void merge() { DefaultDriver one = new DefaultDriver("foo.bar", new ArrayList<>(), "Circus", "lux", "1.2a", ImmutableMap.of(TestBehaviour.class, TestBehaviourImpl.class), ImmutableMap.of("foo", "bar")); Driver ddc = one.merge(new DefaultDriver("foo.bar", new ArrayList<>(), "", "", "", ImmutableMap.of(TestBehaviourTwo.class, TestBehaviourTwoImpl.class), ImmutableMap.of("goo", "wee"))); assertEquals("incorrect name", "foo.bar", ddc.name()); assertEquals("incorrect mfr", "Circus", ddc.manufacturer()); assertEquals("incorrect hw", "lux", ddc.hwVersion()); assertEquals("incorrect sw", "1.2a", ddc.swVersion()); assertEquals("incorrect behaviour count", 2, ddc.behaviours().size()); assertTrue("incorrect behaviour", ddc.hasBehaviour(TestBehaviourTwo.class)); assertEquals("incorrect property count", 2, ddc.properties().size()); assertEquals("incorrect key count", 2, ddc.keys().size()); assertEquals("incorrect property", "wee", ddc.value("goo")); assertTrue("incorrect toString", ddc.toString().contains("Circus")); }
@Override public Driver merge(Driver other) { checkArgument(parents == null || Objects.equals(parent(), other.parent()), "Parent drivers are not the same"); Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours = Maps.newHashMap(); behaviours.putAll(this.behaviours); other.behaviours().forEach(b -> behaviours.put(b, other.implementation(b))); ImmutableMap.Builder<String, String> properties = ImmutableMap.builder(); properties.putAll(this.properties).putAll(other.properties()); List<Driver> completeParents = new ArrayList<>(); if (parents != null) { parents.forEach(parent -> other.parents().forEach(otherParent -> { if (otherParent.name().equals(parent.name())) { completeParents.add(parent.merge(otherParent)); } else if (!completeParents.contains(otherParent)) { completeParents.add(otherParent); } else if (!completeParents.contains(parent)) { completeParents.add(parent); } })); } return new DefaultDriver(name, completeParents.size() > 0 ? completeParents : other.parents(), manufacturer, hwVersion, swVersion, ImmutableMap.copyOf(behaviours), properties.build()); }
DefaultDriver implements Driver { @Override public Driver merge(Driver other) { checkArgument(parents == null || Objects.equals(parent(), other.parent()), "Parent drivers are not the same"); Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours = Maps.newHashMap(); behaviours.putAll(this.behaviours); other.behaviours().forEach(b -> behaviours.put(b, other.implementation(b))); ImmutableMap.Builder<String, String> properties = ImmutableMap.builder(); properties.putAll(this.properties).putAll(other.properties()); List<Driver> completeParents = new ArrayList<>(); if (parents != null) { parents.forEach(parent -> other.parents().forEach(otherParent -> { if (otherParent.name().equals(parent.name())) { completeParents.add(parent.merge(otherParent)); } else if (!completeParents.contains(otherParent)) { completeParents.add(otherParent); } else if (!completeParents.contains(parent)) { completeParents.add(parent); } })); } return new DefaultDriver(name, completeParents.size() > 0 ? completeParents : other.parents(), manufacturer, hwVersion, swVersion, ImmutableMap.copyOf(behaviours), properties.build()); } }
DefaultDriver implements Driver { @Override public Driver merge(Driver other) { checkArgument(parents == null || Objects.equals(parent(), other.parent()), "Parent drivers are not the same"); Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours = Maps.newHashMap(); behaviours.putAll(this.behaviours); other.behaviours().forEach(b -> behaviours.put(b, other.implementation(b))); ImmutableMap.Builder<String, String> properties = ImmutableMap.builder(); properties.putAll(this.properties).putAll(other.properties()); List<Driver> completeParents = new ArrayList<>(); if (parents != null) { parents.forEach(parent -> other.parents().forEach(otherParent -> { if (otherParent.name().equals(parent.name())) { completeParents.add(parent.merge(otherParent)); } else if (!completeParents.contains(otherParent)) { completeParents.add(otherParent); } else if (!completeParents.contains(parent)) { completeParents.add(parent); } })); } return new DefaultDriver(name, completeParents.size() > 0 ? completeParents : other.parents(), manufacturer, hwVersion, swVersion, ImmutableMap.copyOf(behaviours), properties.build()); } @Deprecated  DefaultDriver(String name, Driver parent, String manufacturer,
                         String hwVersion, String swVersion,
                         Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours,
                         Map<String, String> properties);  DefaultDriver(String name, List<Driver> parents, String manufacturer,
                         String hwVersion, String swVersion,
                         Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours,
                         Map<String, String> properties); }
DefaultDriver implements Driver { @Override public Driver merge(Driver other) { checkArgument(parents == null || Objects.equals(parent(), other.parent()), "Parent drivers are not the same"); Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours = Maps.newHashMap(); behaviours.putAll(this.behaviours); other.behaviours().forEach(b -> behaviours.put(b, other.implementation(b))); ImmutableMap.Builder<String, String> properties = ImmutableMap.builder(); properties.putAll(this.properties).putAll(other.properties()); List<Driver> completeParents = new ArrayList<>(); if (parents != null) { parents.forEach(parent -> other.parents().forEach(otherParent -> { if (otherParent.name().equals(parent.name())) { completeParents.add(parent.merge(otherParent)); } else if (!completeParents.contains(otherParent)) { completeParents.add(otherParent); } else if (!completeParents.contains(parent)) { completeParents.add(parent); } })); } return new DefaultDriver(name, completeParents.size() > 0 ? completeParents : other.parents(), manufacturer, hwVersion, swVersion, ImmutableMap.copyOf(behaviours), properties.build()); } @Deprecated  DefaultDriver(String name, Driver parent, String manufacturer,
                         String hwVersion, String swVersion,
                         Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours,
                         Map<String, String> properties);  DefaultDriver(String name, List<Driver> parents, String manufacturer,
                         String hwVersion, String swVersion,
                         Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours,
                         Map<String, String> properties); @Override Driver merge(Driver other); @Override String name(); @Override String manufacturer(); @Override String hwVersion(); @Override String swVersion(); @Override Driver parent(); @Override List<Driver> parents(); @Override Set<Class<? extends Behaviour>> behaviours(); @Override Class<? extends Behaviour> implementation(Class<? extends Behaviour> behaviour); @Override boolean hasBehaviour(Class<? extends Behaviour> behaviourClass); @Override T createBehaviour(DriverData data,
                                                   Class<T> behaviourClass); @Override T createBehaviour(DriverHandler handler,
                                                   Class<T> behaviourClass); @Override Set<String> keys(); @Override String value(String key); @Override Map<String, String> properties(); @Override String toString(); @Override boolean equals(Object driverToBeCompared); @Override int hashCode(); }
DefaultDriver implements Driver { @Override public Driver merge(Driver other) { checkArgument(parents == null || Objects.equals(parent(), other.parent()), "Parent drivers are not the same"); Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours = Maps.newHashMap(); behaviours.putAll(this.behaviours); other.behaviours().forEach(b -> behaviours.put(b, other.implementation(b))); ImmutableMap.Builder<String, String> properties = ImmutableMap.builder(); properties.putAll(this.properties).putAll(other.properties()); List<Driver> completeParents = new ArrayList<>(); if (parents != null) { parents.forEach(parent -> other.parents().forEach(otherParent -> { if (otherParent.name().equals(parent.name())) { completeParents.add(parent.merge(otherParent)); } else if (!completeParents.contains(otherParent)) { completeParents.add(otherParent); } else if (!completeParents.contains(parent)) { completeParents.add(parent); } })); } return new DefaultDriver(name, completeParents.size() > 0 ? completeParents : other.parents(), manufacturer, hwVersion, swVersion, ImmutableMap.copyOf(behaviours), properties.build()); } @Deprecated  DefaultDriver(String name, Driver parent, String manufacturer,
                         String hwVersion, String swVersion,
                         Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours,
                         Map<String, String> properties);  DefaultDriver(String name, List<Driver> parents, String manufacturer,
                         String hwVersion, String swVersion,
                         Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours,
                         Map<String, String> properties); @Override Driver merge(Driver other); @Override String name(); @Override String manufacturer(); @Override String hwVersion(); @Override String swVersion(); @Override Driver parent(); @Override List<Driver> parents(); @Override Set<Class<? extends Behaviour>> behaviours(); @Override Class<? extends Behaviour> implementation(Class<? extends Behaviour> behaviour); @Override boolean hasBehaviour(Class<? extends Behaviour> behaviourClass); @Override T createBehaviour(DriverData data,
                                                   Class<T> behaviourClass); @Override T createBehaviour(DriverHandler handler,
                                                   Class<T> behaviourClass); @Override Set<String> keys(); @Override String value(String key); @Override Map<String, String> properties(); @Override String toString(); @Override boolean equals(Object driverToBeCompared); @Override int hashCode();  }