package io.scalecube.services;

import io.scalecube.services.api.Qualifier;

import java.util.HashMap;
import java.util.Map;

public class ServiceReference {

  private String qualifier;
  private String endpointId;
  private String host;
  private int port;
  private String namespace;
  private String contentType;
  private Map<String, String> tags;
  private String action;
  private CommunicationMode mode;

  /**
   * Constructor for SerDe.
   * 
   * @deprecated exposed only for de/serialization purpose.
   */
  public ServiceReference() {}

  /**
   * Create a new Service reference.
   * 
   * @param serviceMethodDefinition the method definition to build this reference from.
   * @param serviceRegistration a registration for updating the {@link #namespace()} for this
   *        reference
   * @param serviceEndpoint the host and port this reference refers to
   */
  public ServiceReference(ServiceMethodDefinition serviceMethodDefinition,
      ServiceRegistration serviceRegistration,
      ServiceEndpoint serviceEndpoint) {
    this.endpointId = serviceEndpoint.id();
    this.host = serviceEndpoint.host();
    this.port = serviceEndpoint.port();
    this.namespace = serviceRegistration.namespace();
    this.contentType = mergeContentType(serviceMethodDefinition, serviceRegistration);
    this.tags = mergeTags(serviceMethodDefinition, serviceRegistration, serviceEndpoint);
    this.action = serviceMethodDefinition.getAction();
    this.mode = serviceMethodDefinition.getCommunicationMode();
    this.qualifier = Qualifier.asString(namespace, action);
  }

  public CommunicationMode mode() {
    return mode;
  }

  public String qualifier() {
    return this.qualifier;
  }

  public String endpointId() {
    return endpointId;
  }

  public String host() {
    return host;
  }

  public int port() {
    return port;
  }

  public String namespace() {
    return namespace;
  }

  public String contentType() {
    return contentType;
  }

  public Map<String, String> tags() {
    return tags;
  }

  public String action() {
    return action;
  }

  private Map<String, String> mergeTags(ServiceMethodDefinition serviceMethodDefinition,
      ServiceRegistration serviceRegistration,
      ServiceEndpoint serviceEndpoint) {
    Map<String, String> tags = new HashMap<>();
    tags.putAll(serviceEndpoint.tags());
    tags.putAll(serviceRegistration.tags());
    tags.putAll(serviceMethodDefinition.getTags());
    return tags;
  }

  private String mergeContentType(ServiceMethodDefinition serviceMethodDefinition,
      ServiceRegistration serviceRegistration) {
    if (serviceMethodDefinition.getContentType() != null
        && !serviceMethodDefinition.getContentType().isEmpty()) {
      return serviceMethodDefinition.getContentType();
    }
    if (serviceRegistration.contentType() != null && !serviceRegistration.contentType().isEmpty()) {
      return serviceRegistration.contentType();
    }
    throw new IllegalArgumentException();
  }

  @Override
  public String toString() {
    return "ServiceReference{"
        + "qualifier='" + qualifier + '\''
        + ", endpointId='" + endpointId + '\''
        + ", host='" + host + '\''
        + ", port=" + port
        + ", namespace='" + namespace + '\''
        + ", contentType='" + contentType + '\''
        + ", tags=" + tags
        + ", action='" + action + '\''
        + ", mode=" + mode
        + '}';
  }
}

@@ -68,16 +68,55 @@
 alifier;
+%0Aimport io.scalecube.transport.Address;
 %0A%0Aimport
@@ -448,48 +448,41 @@
 de;%0A
-%0A
   
-/**%0A   * Constructor for SerDe.%0A   * 
+private Address address;%0A%0A  /**
 %0A   
@@ -514,17 +514,16 @@
 y for de
-/
 serializ
@@ -579,338 +579,8 @@
 %7B%7D%0A%0A
-  /**%0A   * Create a new Service reference.%0A   * %0A   * @param serviceMethodDefinition the method definition to build this reference from.%0A   * @param serviceRegistration a registration for updating the %7B@link #namespace()%7D for this%0A   *        reference%0A   * @param serviceEndpoint the host and port this reference refers to%0A   */%0A
   pu
@@ -1272,16 +1272,77 @@
 ction);%0A
+    this.address = Address.create(this.host(), this.port());%0A
   %7D%0A%0A  p
@@ -2410,24 +2410,16 @@
  != null
-%0A       
  && !ser
@@ -2803,24 +2803,26 @@
 erence%7B%22
+ +
 %0A       
  + %22qual
@@ -2813,18 +2813,16 @@
 %0A       
- +
  %22qualif
@@ -2842,32 +2842,34 @@
 ifier + '%5C''
+ +
 %0A       
  + %22, endpoi
@@ -2852,26 +2852,24 @@
 '' +%0A       
- +
  %22, endpoint
@@ -2889,32 +2889,34 @@
 intId + '%5C''
+ +
 %0A       
  + %22, host='
@@ -2899,26 +2899,24 @@
 '' +%0A       
- +
  %22, host='%22 
@@ -2924,32 +2924,34 @@
  host + '%5C''
+ +
 %0A       
  + %22, port=%22
@@ -2934,26 +2934,24 @@
 '' +%0A       
- +
  %22, port=%22 +
@@ -2955,24 +2955,26 @@
 %22 + port
+ +
 %0A       
  + %22, na
@@ -2961,26 +2961,24 @@
 rt +%0A       
- +
  %22, namespac
@@ -2996,34 +2996,34 @@
 space + '%5C''
-%0A
  
++%0A
        
-+
  %22, contentT
@@ -3045,34 +3045,34 @@
 tType + '%5C''
-%0A
  
++%0A
        
-+
  %22, tags=%22 +
@@ -3076,24 +3076,26 @@
 %22 + tags
+ +
 %0A       
  + %22, ac
@@ -3082,26 +3082,24 @@
 gs +%0A       
- +
  %22, action='
@@ -3115,24 +3115,26 @@
 n + '%5C''
+ +
 %0A       
  + %22, mo
@@ -3125,18 +3125,16 @@
 %0A       
- +
  %22, mode
@@ -3142,24 +3142,26 @@
 %22 + mode
+ +
 %0A       
  + '%7D';%0A
@@ -3152,18 +3152,16 @@
 %0A       
- +
  '%7D';%0A  
@@ -3161,11 +3161,70 @@
 '%7D';%0A  %7D
+%0A%0A  public Address address() %7B%0A    return this.address;%0A  %7D
 %0A%7D%0A

undo file change