什么时候使用Long vs long在java? [英] When to use Long vs long in java?

查看:634
本文介绍了什么时候使用Long vs long在java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的接口 -

  public interface IDBClient {
public String read(ClientInput input);
}

这是我的界面实现 -



public class DatabaseClient implements IDBClient {

@Override
public String read(ClientInput input){

}
}



现在我有一个工厂获取 DatabaseClient 像这样 -

  IDBClient client = DatabaseClientFactory.getInstance 
....

现在我需要调用读取参数的方法,它接受 ClientInput 参数,一样。这个类不是我写的,所以这是我对这个问题的原因,我很确定这是错误的做法。

  public final class ClientInput {

private Long userid;
private long clientid;
private long timeout_ms = 20L;
private boolean debug;
private map< String,String> parameterMap;

public ClientInput(Long userid,Long clientid,Map< String,String> parameterMap,Long timeout_ms,boolean debug){
this.userid = userid;
this.clientid = clientid;
this.parameterMap = parameterMap;
this.timeout_ms = timeout_ms;
this.debug = debug;
}
}

所以当客户拨打读取方法的方法,它们将创建 ClientInput 参数,使用工厂获取 DatabaseClient 的实例,然后相应地调用读取方法。

  Map< String,String> paramMap = new HashMap< String,String>(); 
paramMap.put(attribute,segmentation);

ClientInput input = new ClientInput(109739281L,20L,paramMap,1000L,true);

IDBClient client = DatabaseClientFactory.getInstance();
client.read(input);

问题陈述:


  1. 所以我的第一个问题是 userid clientid code> timeout_ms 应为 Long 对象或只是 long c $ c> ClientInput class?

  2. 第二个问题是,客户可能会传递错误的信息,例如 negative用户ID 否定客户ID 负超时应该做这个验证吗?我应该在 ClientInput 类的构造函数中还是在其他地方进行此验证检查?


解决方案

认为有一个单一的正确答案。几个建议:




  • 在这个上下文中,long和Long之间最大的区别是Long可能为null。如果有可能你可能缺少值Long对象将有帮助,因为null可以指示缺失值。如果你使用原语,你必须使用一些特殊的值来表示丢失,这可能是一团糟。速度或大小不太可能是一个问题,除非您打算制作一百万个这些内容,然后序列化。


  • 我的偏好设置验证逻辑是在事物可能失败的时候抛出一些自定义的ValidationException。如果你只是用一个构造函数创建这些东西,最简单的事情就是验证那里,例如

      public ClientInput Long用户id,Long clientid,Map< String,String> parameterMap,Long timeout_ms,boolean debug)throws ValidationException {

    if(userid == null)throw new ValidationException(UserId is required);
    ... etc,etc ...
    }



如果您可以在某个地方执行一些有用的操作,

Below is my Interface -

public interface IDBClient {
    public String read(ClientInput input);
}

This is my Implementation of the Interface -

public class DatabaseClient implements IDBClient {

    @Override
    public String read(ClientInput input) {

    }
}

Now I have a factory which gets the instance of DatabaseClient like this -

IDBClient client = DatabaseClientFactory.getInstance();
....

Now I need to make a call to read method of my DatabaseClient which accepts the ClientInput parameter and below is the class for the same. This class was not written by me so that is the reason I am having a question on this and I am pretty much sure this is the wrong way of doing it.

public final class ClientInput {

    private Long userid;
    private Long clientid;
    private Long timeout_ms = 20L;
    private boolean debug;
    private Map<String, String> parameterMap;

    public ClientInput(Long userid, Long clientid, Map<String, String> parameterMap, Long timeout_ms, boolean debug) {
        this.userid = userid;
        this.clientid = clientid;
        this.parameterMap = parameterMap;
        this.timeout_ms = timeout_ms;
        this.debug = debug;
    }
}    

So when customer make a call to read method of DatabaseClient, they will create the ClientInput parameter like this and then use the factory to get the Instance of DatabaseClient and then call the read method accordingly.

Map<String, String> paramMap = new HashMap<String, String>();
paramMap.put("attribute", "segmentation");

ClientInput input = new ClientInput(109739281L, 20L, paramMap, 1000L, true);

IDBClient client = DatabaseClientFactory.getInstance();
client.read(input);

Problem Statement:-

  1. So my first question is does the userid, clientid, timeout_ms should be Long object or just simply long in ClientInput class?
  2. Second question I have is, it might be possible that customer can pass wrong information such as negative user ids, negative client id, negative timeout value etc etc.. Then where I should do this validation? Should I do this validation check in the constructor of ClientInput class or at some other place? What's the better way of doing this and how should I do the validation?

解决方案

I don't think there's a single correct answer. A few suggestions:

  • the biggest difference I see between long and Long in this context is that Long may be null. If there's a possibility you might have missing values the Long object will be helpful as the null can indicate missing values. If you're using primitives you'll have to use some special value to indicate missing, which is probably going to be a mess. Speed or size is not likely to be an issue unless you're planning on making an array of a million of these things and then serializing.

  • My preference for validation logic is to throw some sort of custom ValidationException at the point at which the thing could fail. If you're just creating these things with a constructor, the simplest thing would be just to validate there, e.g.

     public ClientInput(Long userid, Long clientid, Map<String, String> parameterMap, Long timeout_ms, boolean debug) throws ValidationException {          
    
          if (userid == null) throw new ValidationException("UserId is required"); 
                ...etc, etc...
    }
    

    ultimately the validationException is only useful if you can catch it at a point where you can do something useful with it - echo it back to a user or whatever.

这篇关于什么时候使用Long vs long在java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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