001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.client;
019
020import java.util.Collections;
021import java.util.Map;
022import java.util.NavigableMap;
023import java.util.Objects;
024import java.util.Optional;
025import java.util.TreeMap;
026import org.apache.hadoop.hbase.Coprocessor;
027import org.apache.yetus.audience.InterfaceAudience;
028
029/**
030 * Used to build the {@link CoprocessorDescriptor}
031 */
032@InterfaceAudience.Public
033public final class CoprocessorDescriptorBuilder {
034
035  public static CoprocessorDescriptor of(String className) {
036    return new CoprocessorDescriptorBuilder(className).build();
037  }
038
039  public static CoprocessorDescriptorBuilder newBuilder(String className) {
040    return new CoprocessorDescriptorBuilder(className);
041  }
042
043  private final String className;
044  private String jarPath;
045  private int priority = Coprocessor.PRIORITY_USER;
046  // HBASE-29706 Fix hashcode calculation. Use NavigableMap instead of Map
047  private NavigableMap<String, String> properties = new TreeMap<>();
048
049  public CoprocessorDescriptorBuilder setJarPath(String jarPath) {
050    this.jarPath = jarPath;
051    return this;
052  }
053
054  public CoprocessorDescriptorBuilder setPriority(int priority) {
055    this.priority = priority;
056    return this;
057  }
058
059  public CoprocessorDescriptorBuilder setProperty(String key, String value) {
060    this.properties.put(key, value);
061    return this;
062  }
063
064  public CoprocessorDescriptorBuilder setProperties(Map<String, String> properties) {
065    this.properties.putAll(properties);
066    return this;
067  }
068
069  public CoprocessorDescriptor build() {
070    return new CoprocessorDescriptorImpl(className, jarPath, priority, properties);
071  }
072
073  private CoprocessorDescriptorBuilder(String className) {
074    this.className = Objects.requireNonNull(className);
075  }
076
077  private static final class CoprocessorDescriptorImpl implements CoprocessorDescriptor {
078    private final String className;
079    private final String jarPath;
080    private final int priority;
081    private final NavigableMap<String, String> properties;
082
083    private CoprocessorDescriptorImpl(String className, String jarPath, int priority,
084      NavigableMap<String, String> properties) {
085      this.className = className;
086      this.jarPath = jarPath;
087      this.priority = priority;
088      this.properties = properties;
089    }
090
091    @Override
092    public String getClassName() {
093      return className;
094    }
095
096    @Override
097    public Optional<String> getJarPath() {
098      return Optional.ofNullable(jarPath);
099    }
100
101    @Override
102    public int getPriority() {
103      return priority;
104    }
105
106    @Override
107    public NavigableMap<String, String> getProperties() {
108      return Collections.unmodifiableNavigableMap(properties);
109    }
110
111    @Override
112    public String toString() {
113      return "class:" + className + ", jarPath:" + jarPath + ", priority:" + priority
114        + ", properties:" + properties;
115    }
116
117    @Override
118    public boolean equals(Object obj) {
119      if (this == obj) {
120        return true;
121      }
122      if (!(obj instanceof CoprocessorDescriptor)) {
123        return false;
124      }
125      CoprocessorDescriptor other = (CoprocessorDescriptor) obj;
126      if (
127        priority != other.getPriority() || !Objects.equals(className, other.getClassName())
128          || !Objects.equals(getJarPath(), other.getJarPath())
129      ) {
130        return false;
131      }
132      return Objects.equals(getProperties(), other.getProperties());
133    }
134
135    @Override
136    public int hashCode() {
137      return Objects.hash(className, jarPath, priority, properties);
138    }
139  }
140}