Thomas Vachuska
Committed by Gerrit Code Review

Fixing a discrepancy in driver manager implementation.

Change-Id: I774a3ed26dc6d34d6bdc5834e8e38d4d1741c5ed
...@@ -45,19 +45,17 @@ public class DefaultDriverProvider implements DriverProvider { ...@@ -45,19 +45,17 @@ public class DefaultDriverProvider implements DriverProvider {
45 } 45 }
46 46
47 /** 47 /**
48 - * Adds the specified driver to the provider. 48 + * Adds the specified driver to the provider. If a driver with the same
49 + * does not exist yet, the specified one will be added. Otherwise, the
50 + * existing driver will be merged with the new one and the result will be
51 + * registered.
49 * 52 *
50 * @param driver driver to be provided 53 * @param driver driver to be provided
54 + * @return registered driver
51 */ 55 */
52 - public void addDriver(Driver driver) { 56 + public Driver addDriver(Driver driver) {
53 - Driver ddc = drivers.get(driver.name()); 57 + return drivers.compute(driver.name(), (name, oldDriver) ->
54 - if (ddc == null) { 58 + oldDriver == null ? driver : oldDriver.merge(driver));
55 - // If we don't have the driver yet, just use the new one.
56 - drivers.put(driver.name(), driver);
57 - } else {
58 - // Otherwise merge the existing driver with the new one and rebind.
59 - drivers.put(driver.name(), ddc.merge(driver));
60 - }
61 } 59 }
62 60
63 /** 61 /**
......
...@@ -89,10 +89,10 @@ public class DriverManager extends DefaultDriverProvider implements DriverAdminS ...@@ -89,10 +89,10 @@ public class DriverManager extends DefaultDriverProvider implements DriverAdminS
89 @Override 89 @Override
90 public void registerProvider(DriverProvider provider) { 90 public void registerProvider(DriverProvider provider) {
91 provider.getDrivers().forEach(driver -> { 91 provider.getDrivers().forEach(driver -> {
92 - addDrivers(provider.getDrivers()); 92 + Driver d = addDriver(driver);
93 driverByKey.put(key(driver.manufacturer(), 93 driverByKey.put(key(driver.manufacturer(),
94 driver.hwVersion(), 94 driver.hwVersion(),
95 - driver.swVersion()), driver); 95 + driver.swVersion()), d);
96 }); 96 });
97 providers.add(provider); 97 providers.add(provider);
98 } 98 }
...@@ -100,7 +100,7 @@ public class DriverManager extends DefaultDriverProvider implements DriverAdminS ...@@ -100,7 +100,7 @@ public class DriverManager extends DefaultDriverProvider implements DriverAdminS
100 @Override 100 @Override
101 public void unregisterProvider(DriverProvider provider) { 101 public void unregisterProvider(DriverProvider provider) {
102 provider.getDrivers().forEach(driver -> { 102 provider.getDrivers().forEach(driver -> {
103 - removeDrivers(provider.getDrivers()); 103 + removeDriver(driver);
104 driverByKey.remove(key(driver.manufacturer(), 104 driverByKey.remove(key(driver.manufacturer(),
105 driver.hwVersion(), 105 driver.hwVersion(),
106 driver.swVersion())); 106 driver.swVersion()));
...@@ -111,7 +111,6 @@ public class DriverManager extends DefaultDriverProvider implements DriverAdminS ...@@ -111,7 +111,6 @@ public class DriverManager extends DefaultDriverProvider implements DriverAdminS
111 @Override 111 @Override
112 public Set<Driver> getDrivers() { 112 public Set<Driver> getDrivers() {
113 checkPermission(DRIVER_READ); 113 checkPermission(DRIVER_READ);
114 -
115 ImmutableSet.Builder<Driver> builder = ImmutableSet.builder(); 114 ImmutableSet.Builder<Driver> builder = ImmutableSet.builder();
116 drivers.values().forEach(builder::add); 115 drivers.values().forEach(builder::add);
117 return builder.build(); 116 return builder.build();
...@@ -120,7 +119,6 @@ public class DriverManager extends DefaultDriverProvider implements DriverAdminS ...@@ -120,7 +119,6 @@ public class DriverManager extends DefaultDriverProvider implements DriverAdminS
120 @Override 119 @Override
121 public Set<Driver> getDrivers(Class<? extends Behaviour> withBehaviour) { 120 public Set<Driver> getDrivers(Class<? extends Behaviour> withBehaviour) {
122 checkPermission(DRIVER_READ); 121 checkPermission(DRIVER_READ);
123 -
124 return drivers.values().stream() 122 return drivers.values().stream()
125 .filter(d -> d.hasBehaviour(withBehaviour)) 123 .filter(d -> d.hasBehaviour(withBehaviour))
126 .collect(Collectors.toSet()); 124 .collect(Collectors.toSet());
...@@ -129,7 +127,6 @@ public class DriverManager extends DefaultDriverProvider implements DriverAdminS ...@@ -129,7 +127,6 @@ public class DriverManager extends DefaultDriverProvider implements DriverAdminS
129 @Override 127 @Override
130 public Driver getDriver(String driverName) { 128 public Driver getDriver(String driverName) {
131 checkPermission(DRIVER_READ); 129 checkPermission(DRIVER_READ);
132 -
133 return nullIsNotFound(drivers.get(driverName), NO_DRIVER); 130 return nullIsNotFound(drivers.get(driverName), NO_DRIVER);
134 } 131 }
135 132
...@@ -182,7 +179,6 @@ public class DriverManager extends DefaultDriverProvider implements DriverAdminS ...@@ -182,7 +179,6 @@ public class DriverManager extends DefaultDriverProvider implements DriverAdminS
182 @Override 179 @Override
183 public DriverHandler createHandler(DeviceId deviceId, String... credentials) { 180 public DriverHandler createHandler(DeviceId deviceId, String... credentials) {
184 checkPermission(DRIVER_WRITE); 181 checkPermission(DRIVER_WRITE);
185 -
186 Driver driver = getDriver(deviceId); 182 Driver driver = getDriver(deviceId);
187 return new DefaultDriverHandler(new DefaultDriverData(driver, deviceId)); 183 return new DefaultDriverHandler(new DefaultDriverData(driver, deviceId));
188 } 184 }
......