I've read on many Web sites Optional should be used as a return type only, and not used in method arguments. I'm struggling to find a logical reason why. For example I have a piece of logic which has 2 optional parameters. Therefore I think it would make sense to write my method signature like this (solution 1): public int calculateSomething(Optional<String> p1, Optional<BigD
我在很多网站上阅读过可选应该只用作返回类型,而不是在方法参数中使用。 我努力寻找合乎逻辑的原因。 例如,我有一块逻辑有2个可选参数。 因此,我认为这样写我的方法签名是有意义的(解决方案1): public int calculateSomething(Optional<String> p1, Optional<BigDecimal> p2 { // my logic } 许多网页指定可选不应该用作方法参数。 考虑到这一点,我可以使用下面的方法签名并添加一个清晰的Javadoc
It is often the case that a method imposes constraints on its arguments that cannot be described by the type system. For example, a method might require that some argument be non-null, or some int-typed argument be positive. There might also be more complex preconditions, for example that a certain method was called before, or that a certain object is in some state. What is the best way to doc
通常情况下,一种方法对其类型系统无法描述的参数施加约束。 例如,一个方法可能需要某些参数为非空值,或者某些int型参数为正值。 也可能有更复杂的先决条件,例如之前调用某种方法,或某个对象处于某种状态。 在Javadoc中记录这一点的最佳方式是什么? 例如,假设我有以下公共库函数,其中参数不能为负数: public void foo(int bar) { if (bar < 0) { throw new IllegalArgumentException("Negative bars
Reading code of some complex application I thought it could often be helpful to recognize if a method has side effects just by looking on its signature. And after inspecting some of such methods I thought it could be nice to mark them as purely functinal or not in order to make life easier for people reading this code in future. Is there some convention in Java world (javadoc, method naming pa
阅读一些复杂应用程序的代码我认为通过查看其签名来识别方法是否有副作用通常会很有帮助。 在考察了一些这样的方法之后,我认为将它们标记为纯粹的功能可能是不错的,以便将来人们可以更轻松地阅读这些代码。 Java世界中是否存在一些将方法标识为纯函数的约定(javadoc,方法命名模式等)? 你有JavaBeans约定。 参见http://docstore.mik.ua/orelly/java-ent/jnut/ch06_02.htm 。 JavaBeans框架通过建立命名约定来促进
I want to write my custom doclet. I don't want to read some existing javadoc that is made with the standard doclet. I am having problems to figure out how I can query the Javadoc API whether a formal parameter is a varargs paramter. For example if I have the following method: public static void main(String... args) { } How can I determine that the formal parameter args is varargs? I ha
我想写我的自定义doclet。 我不想阅读使用标准doclet制作的一些现有的javadoc。 我有问题想弄清楚如何查询Javadoc API是否一个正式参数是可变参数参数。 例如,如果我有以下方法: public static void main(String... args) { } 我如何确定形参args是可变参数? 我查看了com.sun.javadoc.Type。 但是无法弄清楚如何访问这些信息。 再见 PS:反射没有帮助,因为反射在我猜测的doclet中是不可用的。 在一个doclet中,
If I submit some tasks to an Executor using invokeAll , am I guaranteed that the submitted thread sees all the side effects of the task executions, even if I don't call get() on each of the returned Future s? From a practical point of view, it would seem that this would be a useful guarantee, but I don't see anything in the javadoc. More precisely, do all actions in the body of a Call
如果我使用invokeAll将某些任务提交给Executor ,那么我保证提交的线程能够看到任务执行的所有副作用,即使我没有在每个返回的Future上调用get() 。 从实际的角度来看,这似乎是一个有用的保证,但我没有看到javadoc中的任何东西。 更确切地说,在从invokeAll()调用返回之前, Callable正文中提交给执行器的所有操作都会发生吗? 在每个将来无用地调用get()时都很烦人,事实上返回类型是Void并且没有抛出异常 - 所有工作都
I am wondering when to use static methods? Say if I have a class with a few getters and setters, a method or two, and I want those methods only to be invokable on an instance object of the class. Does this mean I should use a static method? eg Obj x = new Obj(); x.someMethod or Obj.someMethod (is this the static way?) I'm rather confused! One rule-of-thumb: ask yourself "does
我想知道什么时候使用静态方法? 假如我有一个有几个getter和setter的类,一个或两个方法,我希望这些方法只能在类的实例对象上调用。 这是否意味着我应该使用静态方法? 例如 Obj x = new Obj(); x.someMethod 要么 Obj.someMethod (这是静态的方式?) 我很困惑! 一个经验法则是:问问自己“调用这个方法是否合理,即使没有构造Obj?” 如果是这样,它肯定应该是静态的。 所以在Car类中,你可能会有一个double
This question already has an answer here: Iterating through a Collection, avoiding ConcurrentModificationException when removing in loop 21 answers To safely remove from a collection while iterating over it you should use an Iterator. For example: List<String> names = .... Iterator<String> i = names.iterator(); while (i.hasNext()) { String s = i.next(); // must be called bef
这个问题在这里已经有了答案: 循环访问集合,避免在循环21中删除时出现ConcurrentModificationException 在迭代时要安全地从集合中移除,你应该使用Iterator。 例如: List<String> names = .... Iterator<String> i = names.iterator(); while (i.hasNext()) { String s = i.next(); // must be called before you can call i.remove() // Do something i.remove(); } 从Java文档: 这个类的迭
I am trying to understand generics and I purposely want to generate a classcastexception, however, i instead get an arraystoreexception on the first attempt. static <E> E reduce(List<E> list, Function<E> f, E initVal) { E[] snapshot = (E[]) list.toArray(); Object[] o = snapshot; o[0] = new Long(1); E result = initVal; for (E e : snapshot) res
我试图了解泛型,并且我故意要生成一个classCastexception,但是,我反而会在第一次尝试时得到一个数组存储异常。 static <E> E reduce(List<E> list, Function<E> f, E initVal) { E[] snapshot = (E[]) list.toArray(); Object[] o = snapshot; o[0] = new Long(1); E result = initVal; for (E e : snapshot) result = f.apply(result, e); return result; }
Has anyone tried to decipher files encrypted using linux encfs in Java? I'm interested in deciphering the file, and just the file name (not the whole file). I tried: SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); //SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithMD5AndAES"); KeySpec spec = new PBEKeySpec("asdasd".toCharArray(), new String(
有没有人试图破译在Java中使用linux encfs加密的文件? 我对解密文件感兴趣,只是文件名(不是整个文件)。 我试过了: SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); //SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithMD5AndAES"); KeySpec spec = new PBEKeySpec("asdasd".toCharArray(), new String("Ip/6nf5p4Cvg4uocLdIeHJ7uW/Y=").getBytes(), 162752, 192);
I have an entity class User that contains information such as username, first name, last name and a password and I have my GlassFish 3.1 server setup to perform authentication. So far, so good. After the container has authenticated a user, I need some way to bind the principal to the actual User entity. After all, GlassFish is telling me is that user "laurens" has authenticated, it i
我有一个实体类User ,它包含用户名,名字,姓氏和密码等信息,并且我的GlassFish 3.1服务器设置可以执行身份验证。 到现在为止还挺好。 容器验证用户后,我需要一些方法将主体绑定到实际的用户实体。 毕竟,GlassFish告诉我,用户“劳伦斯”已经过身份验证,并不是给我相应的User实体。 为此,我编写了一个JSF托管bean UserController 。 我想知道的是,如果这是查看实际实体的正确方法,以及是否有任何明显的陷阱我没有看