'builder'クラスがあり、Sonarは次の警告を出します。
Missing Static Method In Non Instantiatable Class
pmd : MissingStaticMethodInNonInstantiatableClass
A class that has private constructors and does not have any static methods or fields cannot be used
上記のチェックを満たすために、このクラスをどのようにリファクタリングしますか?私はそのクラスを使用しているので、頭を悩ませています。
使用例:
ViewBuilder vb = new ViewBuilder.Builder()
.modelPart(CONTENT_PAGE, contentPageDao.get(id))
.modelPart("navigation_sections", navigationSectionDao.list() )
.modelPart("available_tags", tagDao.list() )
.modelPart("available_albums", albumDao.list() )
.section(CONTENT_PAGE)
.page("index")
.element("form")
.action("/admin/content_page/save/" + id + ".html")
.build();
クラス自体:
import java.util.HashMap;
import java.util.Map;
public final class ViewBuilder {
private static final String ADMIN_LAYOUT = "admin/layout";
private String layout = ADMIN_LAYOUT;
private String section = "";
private static Map<String, Object> viewParts = new HashMap<String, Object>();
/**
* @return the layout
*/ public String getLayout() {
return layout;
}
/**
* @param layout the layout to set
*/ public void setLayout(String layout) {
this.layout = layout;
}
/**
* @return the section
*/ public String getSection() {
return section;
}
/**
* @param section the section to set
*/ public void setSection(String section) {
this.section = section;
}
public static class Builder{
// required params
private String layout;
private String section;
// optional params
private Map<String, Object > viewParts = new HashMap<String, Object >();
public Builder(){
this.layout = ADMIN_LAYOUT;
viewParts.put("layout", ADMIN_LAYOUT);
}
public Builder( String layout ){
if( layout != null ){
this.layout = layout;
viewParts.put("layout", layout );
} else {
this.layout = ADMIN_LAYOUT;
viewParts.put("layout", ADMIN_LAYOUT);
}
}// constructor
public Builder modelPart( String val, Object o ){
this.viewParts.put(val, o );
return this;
}
public Builder action( String val ){
this.viewParts.put("action", val);
return this;
}
public Builder element( String val ){
this.viewParts.put("element", val);
return this;
}
public Builder section( String val ){
this.section = val;
this.viewParts.put("section", val);
return this;
}
public Builder page( String val ){
this.viewParts.put("page", val);
return this;
}
public Builder layout( String val ){
this.layout = val;
return this;
}
public ViewBuilder build( ){
return new ViewBuilder( this );
}
}// Builder
private ViewBuilder(Builder builder){
this.section = builder.section;
this.layout = builder.layout;
viewParts = builder.viewParts;
}
/**
* Get the value of viewParts
*
* @return the value of viewParts
*/
public Map<String, Object> getViewParts() { return viewParts; }
/**
* Set the value of viewParts
*
* @param viewParts new value of viewParts
*/
public void setViewParts(Map<String, Object> viewParts) { this.viewParts = viewParts; }
}