@Test public void testPut() throws Exception { ServiceInfoCache<ServiceInfo> cache = new ServiceInfoCache<ServiceInfo>(); ServiceURL serviceURL = new ServiceURL("service:foo:bar: Scopes scopes = Scopes.from("scope1"); ServiceInfo serviceInfo1 = new ServiceInfo(serviceURL, Locale.ENGLISH.getLanguage(), scopes, null); ServiceInfoCache.Result<ServiceInfo> result = cache.put(serviceInfo1); assert result.getPrevious() == null; assert cache.getSize() == 1; ServiceInfo serviceInfo = result.getCurrent(); assert serviceInfo == serviceInfo1; ServiceInfo serviceInfo2 = new ServiceInfo(serviceInfo1.getServiceURL(), Locale.GERMAN.getLanguage(), serviceInfo1.getScopes(), serviceInfo1.getAttributes()); result = cache.put(serviceInfo2); assert result.getPrevious() == null; assert cache.getSize() == 2; serviceInfo = result.getCurrent(); assert serviceInfo == serviceInfo2; Attributes attributes = Attributes.from("(attr=sict1)"); ServiceInfo serviceInfo3 = new ServiceInfo(serviceInfo1.getServiceURL(), serviceInfo1.getLanguage(), serviceInfo1.getScopes(), attributes); result = cache.put(serviceInfo3); assert result.getPrevious() == serviceInfo1; assert cache.getSize() == 2; serviceInfo = result.getCurrent(); assert serviceInfo == serviceInfo3; ServiceType serviceType = new ServiceType("service:bar:baz"); ServiceInfo serviceInfo4 = new ServiceInfo(serviceType, serviceInfo1.getServiceURL(), serviceInfo1.getLanguage(), serviceInfo1.getScopes(), serviceInfo1.getAttributes()); try { cache.put(serviceInfo4); assert false; } catch (ServiceLocationException x) { assert x.getSLPError() == SLPError.INVALID_REGISTRATION; } }
public Result<T> put(T service) { check(service); ServiceType serviceType = service.resolveServiceType(); T previous = null; lock(); try { ServiceType existingServiceType = keysToServiceTypes.get(service.getKey()); if (existingServiceType != null && !existingServiceType.equals(serviceType)) throw new ServiceLocationException("Invalid registration of service " + service.getKey() + ": already registered under service type " + existingServiceType + ", cannot be registered also under service type " + serviceType, SLPError.INVALID_REGISTRATION); keysToServiceTypes.put(service.getKey(), serviceType); previous = keysToServiceInfos.put(service.getKey(), service); service.setRegistered(true); if (previous != null) previous.setRegistered(false); } finally { unlock(); } notifyServiceAdded(previous, service); return new Result<T>(previous, service); }
ServiceInfoCache { public Result<T> put(T service) { check(service); ServiceType serviceType = service.resolveServiceType(); T previous = null; lock(); try { ServiceType existingServiceType = keysToServiceTypes.get(service.getKey()); if (existingServiceType != null && !existingServiceType.equals(serviceType)) throw new ServiceLocationException("Invalid registration of service " + service.getKey() + ": already registered under service type " + existingServiceType + ", cannot be registered also under service type " + serviceType, SLPError.INVALID_REGISTRATION); keysToServiceTypes.put(service.getKey(), serviceType); previous = keysToServiceInfos.put(service.getKey(), service); service.setRegistered(true); if (previous != null) previous.setRegistered(false); } finally { unlock(); } notifyServiceAdded(previous, service); return new Result<T>(previous, service); } }
ServiceInfoCache { public Result<T> put(T service) { check(service); ServiceType serviceType = service.resolveServiceType(); T previous = null; lock(); try { ServiceType existingServiceType = keysToServiceTypes.get(service.getKey()); if (existingServiceType != null && !existingServiceType.equals(serviceType)) throw new ServiceLocationException("Invalid registration of service " + service.getKey() + ": already registered under service type " + existingServiceType + ", cannot be registered also under service type " + serviceType, SLPError.INVALID_REGISTRATION); keysToServiceTypes.put(service.getKey(), serviceType); previous = keysToServiceInfos.put(service.getKey(), service); service.setRegistered(true); if (previous != null) previous.setRegistered(false); } finally { unlock(); } notifyServiceAdded(previous, service); return new Result<T>(previous, service); }  }
ServiceInfoCache { public Result<T> put(T service) { check(service); ServiceType serviceType = service.resolveServiceType(); T previous = null; lock(); try { ServiceType existingServiceType = keysToServiceTypes.get(service.getKey()); if (existingServiceType != null && !existingServiceType.equals(serviceType)) throw new ServiceLocationException("Invalid registration of service " + service.getKey() + ": already registered under service type " + existingServiceType + ", cannot be registered also under service type " + serviceType, SLPError.INVALID_REGISTRATION); keysToServiceTypes.put(service.getKey(), serviceType); previous = keysToServiceInfos.put(service.getKey(), service); service.setRegistered(true); if (previous != null) previous.setRegistered(false); } finally { unlock(); } notifyServiceAdded(previous, service); return new Result<T>(previous, service); }  void lock(); void unlock(); void addServiceListener(ServiceListener listener); void removeServiceListener(ServiceListener listener); Result<T> put(T service); int getSize(); T get(ServiceInfo.Key key); Result<T> addAttributes(ServiceInfo.Key key, Attributes attributes); Result<T> removeAttributes(ServiceInfo.Key key, Attributes attributes); Result<T> remove(ServiceInfo.Key key); List<T> match(ServiceType serviceType, String language, Scopes scopes, Filter filter); List<T> getServiceInfos(); List<T> purge(); }
ServiceInfoCache { public Result<T> put(T service) { check(service); ServiceType serviceType = service.resolveServiceType(); T previous = null; lock(); try { ServiceType existingServiceType = keysToServiceTypes.get(service.getKey()); if (existingServiceType != null && !existingServiceType.equals(serviceType)) throw new ServiceLocationException("Invalid registration of service " + service.getKey() + ": already registered under service type " + existingServiceType + ", cannot be registered also under service type " + serviceType, SLPError.INVALID_REGISTRATION); keysToServiceTypes.put(service.getKey(), serviceType); previous = keysToServiceInfos.put(service.getKey(), service); service.setRegistered(true); if (previous != null) previous.setRegistered(false); } finally { unlock(); } notifyServiceAdded(previous, service); return new Result<T>(previous, service); }  void lock(); void unlock(); void addServiceListener(ServiceListener listener); void removeServiceListener(ServiceListener listener); Result<T> put(T service); int getSize(); T get(ServiceInfo.Key key); Result<T> addAttributes(ServiceInfo.Key key, Attributes attributes); Result<T> removeAttributes(ServiceInfo.Key key, Attributes attributes); Result<T> remove(ServiceInfo.Key key); List<T> match(ServiceType serviceType, String language, Scopes scopes, Filter filter); List<T> getServiceInfos(); List<T> purge();  }