PoolingHttpClientConnectionManager setMaxPerRoute(0) and setMaxTotal(0), what will happen? - httpclient

Does someone know if I set MaxPerRoute and MaxTotal of PoolingHttpClientConnectionManager to "0" like below, what will happen?
PoolingHttpClientConnectionManager.setMaxPerRoute(0)
PoolingHttpClientConnectionManager.setMaxTotal(0)

An exception
throw new IllegalArgumentException("Max value may not be negative or zero");
for both methods, check the org.apache.http.pool.AbstractConnPool class source code.

Related

Preconditions checknotnull annotations produce null warnings

The following line
final ProgramObject data =
Preconditions.checkNotNull(datas.get(name), TEMPLATE, name);
gives a warning in android studio
Warning:(291, 44) Argument 'data.get(name)' might be null
When looking at the source code of Preconditions:
#CanIgnoreReturnValue
#NonNullDecl
public static <T extends Object> T checkNotNull(
#NonNullDecl T obj, #NullableDecl String errorMessageTemplate, #NullableDecl Object p1) {
if (obj == null) {
throw new NullPointerException(lenientFormat(errorMessageTemplate, p1));
}
return obj;
}
It looks like the first parameter is not allowed to get null.
Here is the PR connected to it:
https://github.com/google/guava/commit/a890c444e55973384d1370b56afe1a02e7db9c3c
So i wonder:
Is there something in Android studio which i did not configure well
Is this a bug in guava?
Obviously if i make a null check i suspect that the parameter can be null
The intent of Preconditions.checkNotNull is that it should only be used on variables that you believe can never be null -- and you want to make sure your belief is correct, and have an exception thrown if you were wrong.
Guava's setup is working as it intended. It may be appropriate for you to suppress the warning.

"Useless expression" warning which I think is not correct

Consider following code sample:
def cleanUpAdvertiserInAdtech(name: String)(implicit driver: WebDriver):Unit = {
AdtechLoginPage.open
AdtechLoginPage.login
val attempt = Try {
AdtechDashboardPage.openAdvertisers
AdtechAdvertisersPage.deleteAdvertiserIfExists(name)
}
AdtechAdvertisersPage.logout
attempt.get
}
IntelliJ IDEA underlines the last line attempt.get and says that this is a useless expression. I am not sure I understand exactly why because this line just returns Unit in case when everything was nice in the Try{...} but throws an exception when something went wrong there.
Could you explain?
Because attempt.get not only will return Success value, it also will throw Failure exception:
If the Try.get return success value, attempt.get will return a success value, but it's never used. so this warning is correct to throw.
If the Try.get is failed,attempt.get will throw an Exception, the IDE warning is useless.
So I think IDE is correct to highlight this warning(IDE doesn't know whether will fail). you should change the Exception evaluate style, like use: match or try catch finally.

Working with Scala Option

I have been changing a lot of my Scala code recently to avoid instantiating variables with null and instead using Option. For example, I previously had:
var cxn: RepositoryConnection = null
cxn = repo.getConnection()
//do something with the connection, then close it
cxn.close()
Now, my code looks more like this.
var cxn = None : Option[RepositoryConnection]
cxn = Some(repo.getConnection())
//do something with the connection, then close it
Now the problem I have is when I want to call a method associated with the RepositoryConnection type. I try:
cxn.close()
and see this error:
value close is not a member of Option[org.openrdf.repository.RepositoryConnection]
Now, when I was using null, this operation worked just fine, because cxn was a RepositoryConnection object, not an Option[RepositoryConnection]. Is there an easy way to call the close() method now that I am using Option?
You have a few options. (Sorry about the pun.) The most straight forward is probably...
cxn.map(_.close())
But perhaps you need to do something else if cxn is None. Then you could do something like...
cxn.fold(logger.reportStatus())(_.close())
Since your variable is Option[Something], you can not call instanceOfSomethingOpt.methodOfInstance()
Instead do instanceOfSomethingOpt.map(realInstance => realInstance.methodOfInstance())
In your case, it'd be
cxn.map(realConnection => realConnection.close())
//or to make it shorter
cxn.map(_.close())
You should really give a look at Option api.
cxn.map(_.close())
is one way, in case close() returns something you might beed.
cxn.foreach(_.close())
is another way, if close() is not doing much (side-effect).
val cxn = Some(repo.getConnection())
for (c <- cxn) yield {
//do something with the connection
c.close()
}
Alternatively you can wrap the getConnection with Either or Try depending on how you want to handle errors see http://blog.xebia.com/try-option-or-either/

Conditional exception raising in a flow block

While using Akka's data-flow DSL, I have twice encountered a need to throw an exception inside future, conditionally. This is how I am doing it:
flow {
// ...
if (someCond)
shiftUnit(throw new SomeException)
else
Future().apply()
// ...
}
Is this the correct way to do it? Or is there a better approach?
The approach seems correct (although my knowledge is a bit rusty), you can even leave out the other branch, the following works for me (Scala 2.10.1):
flow { if (x == 2) shiftUnit(throw new Exception) }
which results in a Future[Unit].

Early return from a Scala constructor

I am writing the constructor for my "main" class. The first thing it does is call a method to use commons-cli to parse the command line. If the parseOptions method returns false, an error has occurred, and the constructor should exit.
I tried writing the following code
if (!parseOptions(args)) return
but the compiler complains that I have a "Return statement outside method definition".
Short of calling System.exit(1) or inverting the boolean (and putting all of the rest of my logic inside the if statement, is there any way to return "early" from a constructor?
I suppose I could have the parseOptions method throw an IllegalArgumentException and catch that in my Main object.
Thanks.
Dont try to do a early/premature return, this makes your code harder more complex, since the side effects of the return can be hard to understand. Instead use a exception to signal that something is wrong.
You can use require in the constructor. This doesn't return. But it seems like throwing an exception actually fits his situation better.
As in:
class MyTest(
private var myValue: Int ){
require(myValue > 0) // Connected to constructor
}
defined class MyTest
scala> val x = new MyTest(10)
x: MyTest = MyTest#49ff4282
scala> val y = new MyTest(-10)
java.lang.IllegalArgumentException: requirement failed
at scala.Predef$.require(Predef.scala:133)
is there any way to return "early" from a constructor
No. But in your case it sounds like bad design, anyway.
If the parseOptions method returns false, an error has occurred
In this case the constructor should throw an exception, not return normally.
A constructor should always either complete fully, or abort (throw an exception). Anything else leaves your object "half constructed" and thus impossible to reason about.
If in your case, the object is valid even if parseOptions failed, then you can change the condition and continue:
if (parseOptions(args)) {
// rest of constructor
}