创建一个类型为< String,Object>的HashMap [英] Creating a HashMap of type <String , Object>

查看:393
本文介绍了创建一个类型为< String,Object>的HashMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在上一篇文章中创建ToolTip托管bean



我能够创建一个管理bean来收集和显示工具提示文本,只需要一次查找并将它们存储在Application Scope变量中。这很好。



我在JAVA学习曲线的相当陡峭的部分,请原谅我。



我有另一个托管bean要求创建一个HashMap应用程序范围,但是这次它需要是一个String类型的对象。该应用程序是我拥有一个包含大部分代码,自定义控件和XPage的主数据库的地方。此主数据库将指向一个或多个应用程序数据库,该数据库将存储特定于相关应用程序的Notes文档。因此,我在主文件中创建了一系列应用程序文档,这些文档指定了应用程序特定的应用程序,帮助和规则数据库的RepID以及有关应用程序的许多其他信息。这应该允许我重复使用相同的自定义控件,通过将应用程序名称传递给特定的数据库。作为一个例子,Master Design DB可能会指向Purchasing,Customer Complaints,Travel Requests等。
下面的代码用于加载和存储HashMap,但我无法检索数据。
编译器在公共Object get(String key)方法中显示两个错误,而在mapValue = this.internalMap.get(key);显示两个错误。在getAppRepID方法中,我认为它主要是语法,但不确定。

  / ** 
*这个类使得变量在Workflo!中定义一个应用程序!批准
*作为ApplicationScope变量提供。
* /
package ca.wfsystems.wfsAppUtils;

导入lotus.domino.Base;
导入lotus.domino.Session;
导入lotus.domino.Database;
导入lotus.domino.View;
导入lotus.domino.NotesException;
导入lotus.domino.ViewColumn;
导入lotus.domino.ViewEntry;
导入lotus.domino.ViewEntryCollection;
导入lotus.domino.Name;


import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import com.ibm.xsp.extlib.util.ExtLibUtil;

/ **
* @author Bill Fox Workflo Systems WFSystems.ca
* 2014年7月
*本课程作为Workflo的一部分提供! b $ b *并且可以在此应用程序中重复使用。
*如果复制到其他应用程序,请保留此属性。
*
* /
public abstract class ApplicationUtils implements Serializable,Map< String,Object> {

private static final long serialVersionUID = 1L;
私人会话s;
私人名称serverName;
private String repID;
private String thisKey;
private ViewEntryCollection formVECol;
私人向量formNames;
private数据库thisDB;
私人数据库appDB;
私人查看appView;
private查看formView;
private ViewEntry formVE;
private ViewEntry tFormVE;
private ViewEntry ve;
私人ViewEntry tVE;
private ViewEntryCollection veCol;
private final Map< String,Object> internalMap = new HashMap< String,Object>();

public ApplicationUtils(){
this.populateMap(internalMap);


private void populateMap(Map< String,Object> theMap){

try {
s = ExtLibUtil.getCurrentSession();
// serverName = s.createName(s.getServerName());
thisDB = s.getCurrentDatabase();
appView = thisDB.getView(vwWFSApplications);
veCol = appView.getAllEntries();
ve = veCol.getFirstEntry();
ViewEntry tVE = null; (ve!= null){
rtnValue mapValue = new rtnValue();
tVE = veCol.getNextEntry(ve);
Vector colVal = ve.getColumnValues();
thisKey = colVal.get(0).toString();
mapValue.setRepID(colVal.get(2).toString());
// ......加载其余的值.......
theMap.put(thisKey,mapValue);
recycleObjects(ve);
ve = tVE;
}

} catch(NotesException e){
System.out.println(e.toString());
} finally {
recycleObjects(ve,veCol,appView,tVE);
}
}

public class rtnValue {
private String RepID;
private String HelpRepID;
private String RuleRepID;
私人向量FormNames;


public String getRepID(){
return RepID;
}
public void setRepID(String repID){
RepID = repID;
}
public String getHelpRepID(){
return HelpRepID;
}
public void setHelpRepID(String helpRepID){
HelpRepID = helpRepID;
}
public String getRuleRepID(){
return RuleRepID;
}
public void setRuleRepID(String ruleRepID){
RuleRepID = ruleRepID;
}
public Vector getFormNames(){
return FormNames;
}
public void setFormNames(Vector formNames){
FormNames = formNames;
}
}

public void clear(){
this.internalMap.clear();
this.populateMap(this.internalMap);
}

public boolean containsKey(Object key){
return this.internalMap.containsKey(key);
}

public boolean containsValue(Object value){
return this.internalMap.containsValue(value);
}

public设置< java.util.Map.Entry< String,Object>> entrySet(){
return this.internalMap.entrySet();



public Object get(String key){
// Object get方法的错误必须返回Object类型的结果
try {
if(this.internalMap.containsKey(key)){
返回this.internalMap.get(key);
}
} catch(Exception e){
System.out.println(e.toString());
rtnValue newMap = new rtnValue();
返回newMap;




$ b public boolean isEmpty(){
return this.internalMap.isEmpty();
}

public Set< String> keySet(){
return this.internalMap.keySet();
}

public Object put(String key,Object value){
return this.internalMap.put(key,value);
}



public Object remove(Object key){
return this.internalMap.remove(key);
}

public int size(){
return this.internalMap.size();
}

public Collection< Object> values(){
return this.internalMap.values();

$ b $ public void putAll(Map< ;? extends String,?extends Object> m){
this.internalMap.putAll(m);
}

public String getAppRepID(String key){
/ *获取应用程序数据库的副本标识
*不确定这是调用此方法的正确方式
* /
rtnValue mapValue = new rtnValue();
mapValue = this.internalMap.get(key);
//上面的错误类型不匹配:无法将Object转换为ApplicationUtils.rtnValue
String repID = mapValue.getRepID();

$ b public static void recycleObjects(Object ... args){
for(Object o:args){
if(o!= null){
if(o instanceof Base){
try {
((Base)o).recycle();
} catch(Throwable t){
//谁在乎?
}
}
}
}
}
}


解决方案

对于get()方法,我处理这种情况的方式是创建一个正确数据类型为null的变量,在我的try / catch设置变量,并在最后返回变量。所以:

  Object retVal = null; 
尝试....
返回retVal;

对于其他错误,如果右键单击错误标记,可能会给您机会将变量强制转换为rtnValue,如下所示:

pre $ map $ value $ mapValue =(rtnValue)this.internalMap.get(key)

如果你还没有掌握它,Head First Java是一本有用的书,可以让我了解Java的一些概念。还值得从OpenNTF下载Domino Designer的FindBugs插件。它将识别错误以及不良做法。只要忽略本地软件包中的错误即可!


In a previous post Creating a ToolTip Managed bean

I was able to create a manged bean to collect and display tooltip text with only a single lookup and store them in an Application Scope variable. This has worked great.

I am on the rather steep part of the JAVA learning curve so please forgive me.

I have another managed bean requirement to create a HashMap Application Scope but this time it needs to be of a type String, Object. The application is where I have a single 'master' database that contains most of the code, custom controls, and XPages. This Master Database will point to One or More application databases that will store the Notes Documents specific to the application in question. So I have created in the Master a series of Application Documents that specify the RepIDs of the Application, Help and Rules databases specific to the Application along with a number of other pieces of information about the Application. This should allow me to reuse the same custom control that will open the specific DB by passing it the Application Name. As an example the Master Design DB might point to "Purchasing", "Customer Complaints", "Travel Requests" etc. The code below works to load and store the HashMap, but I am having trouble retrieving the the data. The compiler shows two errors one at the public Object get(String key) method and the other at mapValue = this.internalMap.get(key); in the getAppRepID method I think that it is mainly syntax but not sure. I have commented the error in the code where it appears.

/**
 *This Class makes the variables that define an application within Workflo!Approval
 *available as an ApplicationScope variable. 
 */
package ca.wfsystems.wfsAppUtils;

import lotus.domino.Base;
import lotus.domino.Session;
import lotus.domino.Database;
import lotus.domino.View;
import lotus.domino.NotesException;
import lotus.domino.ViewColumn;
import lotus.domino.ViewEntry;
import lotus.domino.ViewEntryCollection;
import lotus.domino.Name;


import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import com.ibm.xsp.extlib.util.ExtLibUtil;

/**
 * @author Bill Fox Workflo Systems WFSystems.ca
 * July 2014
 * This class is provided as part of the Workflo!Approval Product
 * and can be reused within this application.
 * If copied to a different application please retain this attribution.
 *
 */
public abstract class ApplicationUtils implements Serializable, Map<String, Object> {

    private static final long serialVersionUID = 1L;
    private Session s;
    private Name serverName;
    private String repID;
    private String thisKey;
    private ViewEntryCollection formVECol;
    private Vector formNames;
    private Database thisDB;
    private Database appDB;
    private View appView;
    private View formView;
    private ViewEntry formVE;
    private ViewEntry tFormVE;
    private ViewEntry ve;
    private ViewEntry tVE;
    private ViewEntryCollection veCol;
    private final Map<String, Object> internalMap = new HashMap<String, Object>();

    public ApplicationUtils() {
        this.populateMap(internalMap);
    }

    private void populateMap(Map<String, Object> theMap) {

        try{
            s = ExtLibUtil.getCurrentSession();
            //serverName = s.createName(s.getServerName());
            thisDB = s.getCurrentDatabase();
            appView = thisDB.getView("vwWFSApplications");
            veCol = appView.getAllEntries();
            ve = veCol.getFirstEntry();
            ViewEntry tVE = null;
            while (ve != null) {
                rtnValue mapValue = new rtnValue();
                tVE = veCol.getNextEntry(ve);
                Vector colVal = ve.getColumnValues();
                thisKey = colVal.get(0).toString();
                mapValue.setRepID(colVal.get(2).toString());
                // ...... load the rest of the values .......
                theMap.put(thisKey, mapValue);  
                recycleObjects(ve);
                ve = tVE;
            }

        }catch(NotesException e){
            System.out.println(e.toString());
        }finally{
            recycleObjects(ve, veCol, appView, tVE);
        }
    }

    public class rtnValue{
        private String RepID;
        private String HelpRepID;
        private String RuleRepID;
        private Vector FormNames;


        public String getRepID() {
            return RepID;
        }
        public void setRepID(String repID) {
            RepID = repID;
        }
        public String getHelpRepID() {
            return HelpRepID;
        }
        public void setHelpRepID(String helpRepID) {
            HelpRepID = helpRepID;
        }
        public String getRuleRepID() {
            return RuleRepID;
        }
        public void setRuleRepID(String ruleRepID) {
            RuleRepID = ruleRepID;
        }
        public Vector getFormNames() {
            return FormNames;
        }
        public void setFormNames(Vector formNames) {
            FormNames = formNames;
        }
    }

    public void clear() {
        this.internalMap.clear();
        this.populateMap(this.internalMap);
    }

    public boolean containsKey(Object key) {
        return this.internalMap.containsKey(key);
    }

    public boolean containsValue(Object value) {
        return this.internalMap.containsValue(value);
    }

    public Set<java.util.Map.Entry<String, Object>> entrySet() {
        return this.internalMap.entrySet();

    }

    public Object get(String key) {
        //error on Object get Method must return a result of type Object
        try {
            if (this.internalMap.containsKey(key)) {
                return this.internalMap.get(key);
            }
        } catch (Exception e) {
            System.out.println(e.toString());
            rtnValue newMap = new rtnValue();
            return newMap;

        }

    }

    public boolean isEmpty() {
        return this.internalMap.isEmpty();
    }

    public Set<String> keySet() {
        return this.internalMap.keySet();
    }

    public Object put(String key, Object value) {
        return this.internalMap.put(key, value);
    }



    public Object remove(Object key) {
        return this.internalMap.remove(key);
    }

    public int size() {
        return this.internalMap.size();
    }

    public Collection<Object> values() {
        return this.internalMap.values();
    }

    public void putAll(Map<? extends String, ? extends Object> m) {
        this.internalMap.putAll(m);
    }

    public String getAppRepID(String key){
        /*get the Replica Id of the application database 
         * not sure this is the correct way to call this
         */
        rtnValue mapValue = new rtnValue();
        mapValue = this.internalMap.get(key);
        //error on line above Type Mismatch: can not convert Object to ApplicationUtils.rtnValue
        String repID = mapValue.getRepID();

    }
    public static void recycleObjects(Object... args) {
        for (Object o : args) {
            if (o != null) {
                if (o instanceof Base) {
                    try {
                        ((Base) o).recycle();
                    } catch (Throwable t) {
                        // who cares?
                    }
                }
            }
        }
    }
}

解决方案

For the get() method, the way I handle that kind of situation is create a variable of the correct data type as null, in my try/catch set the variable, and at the end return the variable. So:

Object retVal = null;
try....
return retVal;

For the other error, if you right-click on the error marker, it might give you the opportunity to cast the variable to rtnValue, so:

mapValue = (rtnValue) this.internalMap.get(key)

If you haven't got it, Head First Java was a useful book for getting my head around some Java concepts. It's also worth downloading the FindBugs plugin for Domino Designer from OpenNTF. It will identify errors as well as bad practices. Just ignore the errors in the "local" package!

这篇关于创建一个类型为&lt; String,Object&gt;的HashMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆