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.Objects; 023import java.util.Optional; 024import java.util.TreeMap; 025import org.apache.hadoop.hbase.Coprocessor; 026import org.apache.yetus.audience.InterfaceAudience; 027 028/** 029 * Used to build the {@link CoprocessorDescriptor} 030 */ 031@InterfaceAudience.Public 032public final class CoprocessorDescriptorBuilder { 033 034 public static CoprocessorDescriptor of(String className) { 035 return new CoprocessorDescriptorBuilder(className).build(); 036 } 037 038 public static CoprocessorDescriptorBuilder newBuilder(String className) { 039 return new CoprocessorDescriptorBuilder(className); 040 } 041 042 private final String className; 043 private String jarPath; 044 private int priority = Coprocessor.PRIORITY_USER; 045 private Map<String, String> properties = new TreeMap(); 046 047 public CoprocessorDescriptorBuilder setJarPath(String jarPath) { 048 this.jarPath = jarPath; 049 return this; 050 } 051 052 public CoprocessorDescriptorBuilder setPriority(int priority) { 053 this.priority = priority; 054 return this; 055 } 056 057 public CoprocessorDescriptorBuilder setProperty(String key, String value) { 058 this.properties.put(key, value); 059 return this; 060 } 061 062 public CoprocessorDescriptorBuilder setProperties(Map<String, String> properties) { 063 this.properties.putAll(properties); 064 return this; 065 } 066 067 public CoprocessorDescriptor build() { 068 return new CoprocessorDescriptorImpl(className, jarPath, priority, properties); 069 } 070 071 private CoprocessorDescriptorBuilder(String className) { 072 this.className = Objects.requireNonNull(className); 073 } 074 075 private static final class CoprocessorDescriptorImpl implements CoprocessorDescriptor { 076 private final String className; 077 private final String jarPath; 078 private final int priority; 079 private final Map<String, String> properties; 080 081 private CoprocessorDescriptorImpl(String className, String jarPath, int priority, 082 Map<String, String> properties) { 083 this.className = className; 084 this.jarPath = jarPath; 085 this.priority = priority; 086 this.properties = properties; 087 } 088 089 @Override 090 public String getClassName() { 091 return className; 092 } 093 094 @Override 095 public Optional<String> getJarPath() { 096 return Optional.ofNullable(jarPath); 097 } 098 099 @Override 100 public int getPriority() { 101 return priority; 102 } 103 104 @Override 105 public Map<String, String> getProperties() { 106 return Collections.unmodifiableMap(properties); 107 } 108 109 @Override 110 public String toString() { 111 return "class:" + className + ", jarPath:" + jarPath + ", priority:" + priority 112 + ", properties:" + properties; 113 } 114 } 115}