package io.grpc; import com.google.common.base.Preconditions; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.Map; /* loaded from: classes6.dex */ public final class ServerServiceDefinition { private final Map> methods; private final ServiceDescriptor serviceDescriptor; public static Builder builder(String str) { return new Builder(str); } public static Builder builder(ServiceDescriptor serviceDescriptor) { return new Builder(serviceDescriptor); } private ServerServiceDefinition(ServiceDescriptor serviceDescriptor, Map> map) { this.serviceDescriptor = (ServiceDescriptor) Preconditions.checkNotNull(serviceDescriptor, "serviceDescriptor"); this.methods = Collections.unmodifiableMap(new HashMap(map)); } public final Collection> getMethods() { return this.methods.values(); } public final ServerMethodDefinition getMethod(String str) { return this.methods.get(str); } /* loaded from: classes6.dex */ public static final class Builder { private final Map> methods; private final ServiceDescriptor serviceDescriptor; private final String serviceName; private Builder(String str) { this.methods = new HashMap(); this.serviceName = (String) Preconditions.checkNotNull(str, "serviceName"); this.serviceDescriptor = null; } private Builder(ServiceDescriptor serviceDescriptor) { this.methods = new HashMap(); this.serviceDescriptor = (ServiceDescriptor) Preconditions.checkNotNull(serviceDescriptor, "serviceDescriptor"); this.serviceName = serviceDescriptor.getName(); } public final Builder addMethod(MethodDescriptor methodDescriptor, ServerCallHandler serverCallHandler) { return addMethod(ServerMethodDefinition.create((MethodDescriptor) Preconditions.checkNotNull(methodDescriptor, "method must not be null"), (ServerCallHandler) Preconditions.checkNotNull(serverCallHandler, "handler must not be null"))); } public final Builder addMethod(ServerMethodDefinition serverMethodDefinition) { MethodDescriptor methodDescriptor = serverMethodDefinition.getMethodDescriptor(); Preconditions.checkArgument(this.serviceName.equals(methodDescriptor.getServiceName()), "Method name should be prefixed with service name and separated with '/'. Expected service name: '%s'. Actual fully qualifed method name: '%s'.", this.serviceName, methodDescriptor.getFullMethodName()); String fullMethodName = methodDescriptor.getFullMethodName(); Preconditions.checkState(!this.methods.containsKey(fullMethodName), "Method by same name already registered: %s", fullMethodName); this.methods.put(fullMethodName, serverMethodDefinition); return this; } public final ServerServiceDefinition build() { ServiceDescriptor serviceDescriptor = this.serviceDescriptor; if (serviceDescriptor == null) { ArrayList arrayList = new ArrayList(this.methods.size()); Iterator> it = this.methods.values().iterator(); while (it.hasNext()) { arrayList.add(it.next().getMethodDescriptor()); } serviceDescriptor = new ServiceDescriptor(this.serviceName, arrayList); } HashMap hashMap = new HashMap(this.methods); for (MethodDescriptor methodDescriptor : serviceDescriptor.getMethods()) { ServerMethodDefinition serverMethodDefinition = (ServerMethodDefinition) hashMap.remove(methodDescriptor.getFullMethodName()); if (serverMethodDefinition == null) { StringBuilder sb = new StringBuilder("No method bound for descriptor entry "); sb.append(methodDescriptor.getFullMethodName()); throw new IllegalStateException(sb.toString()); } if (serverMethodDefinition.getMethodDescriptor() != methodDescriptor) { StringBuilder sb2 = new StringBuilder("Bound method for "); sb2.append(methodDescriptor.getFullMethodName()); sb2.append(" not same instance as method in service descriptor"); throw new IllegalStateException(sb2.toString()); } } if (hashMap.size() > 0) { StringBuilder sb3 = new StringBuilder("No entry in descriptor matching bound method "); sb3.append(((ServerMethodDefinition) hashMap.values().iterator().next()).getMethodDescriptor().getFullMethodName()); throw new IllegalStateException(sb3.toString()); } return new ServerServiceDefinition(serviceDescriptor, this.methods); } } public final ServiceDescriptor getServiceDescriptor() { return this.serviceDescriptor; } }