001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase.client;
020
021import java.util.Collections;
022import java.util.Map;
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  private Map<String, String> properties = new TreeMap();
047
048  public CoprocessorDescriptorBuilder setJarPath(String jarPath) {
049    this.jarPath = jarPath;
050    return this;
051  }
052
053  public CoprocessorDescriptorBuilder setPriority(int priority) {
054    this.priority = priority;
055    return this;
056  }
057
058  public CoprocessorDescriptorBuilder setProperty(String key, String value) {
059    this.properties.put(key, value);
060    return this;
061  }
062
063  public CoprocessorDescriptorBuilder setProperties(Map<String, String> properties) {
064    this.properties.putAll(properties);
065    return this;
066  }
067
068  public CoprocessorDescriptor build() {
069    return new CoprocessorDescriptorImpl(className, jarPath, priority, properties);
070  }
071
072  private CoprocessorDescriptorBuilder(String className) {
073    this.className = Objects.requireNonNull(className);
074  }
075
076  private static final class CoprocessorDescriptorImpl implements CoprocessorDescriptor {
077    private final String className;
078    private final String jarPath;
079    private final int priority;
080    private final Map<String, String> properties;
081
082    private CoprocessorDescriptorImpl(String className, String jarPath, int priority,
083      Map<String, String> properties) {
084      this.className = className;
085      this.jarPath = jarPath;
086      this.priority = priority;
087      this.properties = properties;
088    }
089
090    @Override
091    public String getClassName() {
092      return className;
093    }
094
095    @Override
096    public Optional<String> getJarPath() {
097      return Optional.ofNullable(jarPath);
098    }
099
100    @Override
101    public int getPriority() {
102      return priority;
103    }
104
105    @Override
106    public Map<String, String> getProperties() {
107      return Collections.unmodifiableMap(properties);
108    }
109
110    @Override
111    public String toString() {
112      return "class:" + className
113        + ", jarPath:" + jarPath
114        + ", priority:" + priority
115        + ", properties:" + properties;
116    }
117  }
118}