Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!

Maxim Karvonen

Ranch Hand
+ Follow
since Jun 14, 2013
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Maxim Karvonen

Hi, Chris

I suggest using Seq.flatMap + Try.toOption:

If your are not interested in errors at all, I suggest using your own method returning an option and ignoring errors:
4 years ago
Hi, Fernando

Fernando Skackauskas wrote:
Why the JavaScript can convert, for example, the Hex character "df" as referring to "ß" and Java can not?

Why do you think than Java can't do that? It can and actually do. But file does not contain characters, it contains bytes. And there are many ways to convert the same character into different set of bytes (those conversions are called "encodings"). That conversion happens in the PrintWriter. As you does not pass an explicit encoding to use, some "default platform encoding" is used to convert characters into bytes. Then your editor reads those bytes and uses a different encoding to convert them back to characters.

Looks like you should not use string manipulations at all. Your file is not text file, it is binary file. So you have to use binary output streams (like FileOutputStream) and just write decoded bytes (not characters).
4 years ago

Stephan van Hulst wrote:Personally I've never really understood the reason star imports are discouraged so much.

Assume you are using version 1.1.2 of my library. You have imports like:

It compiles and works well. Over two months I improve my library. I only add new methods and classes and do not change any existing behaviour. Those changes are backward-compatible. Then I release a version 1.2. You read release notes for the version 1.2 and see some new method you really need. So you decide to upgrade. You change a library/build specification. Then you compile your project and get a very confusing error stating that usage of "File" is ambigous in YourClass. Turns out that I have added a File into ru.maxkar.some.library package.

Yes, compiler would warn you about ambiguity. But would it be easy for you to resolve? You may forget what was supposed to be used in the file. My File have an API similar to API. So YourClass could be compiled with both my File and JDK File. And you'll have to find users of YourClass to figue out which exactly File was passed in a call to

Of course, you could still read a full change log of my library and figure out the offending class. But what if your are updating many libraries simultaneously?

Creating "File" class was not a best my idea and probably I would not do that. But the similar scenario could arise with other classes and third-party libraries.
4 years ago
Hi, Jasper

It's not a JavaFX limitation. It is a hardware limitation. Most keyboards do not support 4 simultaneous keypresses. Some of them do not support even some 3-key combinations.
4 years ago
Hi, Anka

My suggestion is that your are closing file output stream before deflater output stream. Deflater have an internal buffer and process data in "batches" by compressing them. It ocasionally write data and also finishes compression and flushes the buffer when close() method is called. However, file stream is closed one line above so all the remaining data are lost and the resulting file is "truncated". Switching an order should help. Or even better, you do not have to close the file stream by hand. DeflaterOutputStream will close the underlying stream when dos.close() is called.

BTW, do you get any exceptions running your program? An exception should happen in line 17, where DeflaterOutputStream tries to write into the already-closed file stream. So the code should say "Exception in Compress/Decompress program".
4 years ago
Hi, Rajesh

This is a very good question.

The type of the value variable is not what you expect! It is not your webservice.Value. It is com.fasterxml.jackson.annotation.JsonFormat.Value. That's why toString in the webservice.Value is never called - these values have never been part of the Quote.
Imported type always shadows types in the same package, see JLS, 6.4.1. Relevant section:

A single-static-import declaration d in a compilation unit c of package p that imports a type named n shadows, throughout c, the declarations of:
* ...
* any top level type (§7.6) named n declared in another compilation unit (§7.3) of p;

In your example solution is simple: just remove an offending import. If it is not possible (both classes are needed), you have to use fully-qualified names (i.e. full type name with its package) for all but one types with the same simple name (and you could import only that required one).
4 years ago
Hi, Piet

I do not know easy way to do that using your class strucutre (I believe there is no Scala syntax to do that).

However, there is a quite simple solution. I usually do this in the following way

In this case methods could use only structure when they need this. And they could pattern-match with operator type (case BinaryExpr(Plus, _, _)) when they need.
5 years ago
Hi, Emrah

Do you input your first command in a shell? If yes, then you do not need any additional quotes in parameters passed to a ProcessBuilder. Quotes are treated in a special way by the shell. They are used to properly pass some arguments. For example, shell treats ""{?s ?p ?o}" as single argument. Without quotes it would be three arguments. However, in ProcessBuilder you already have natural separation between arguments. Each string is treated as exactly one parameter. So in your code you should have "{?s ?p ?o}" instead of "\"{?s ?p ?o}\"". Same for "S".
5 years ago
Hi, John

John O'Donoghue wrote:
Under the heading JDBC Connection Properties in Eclipse, I chose the option of Derby Embedded JDBC Driver. Is that the same as the "JDBC transaction with a proper transaction isolation level" property that you mention?

No, it is not the same. Derby stands for database and its corresponding driver. Transactions and isolation levels are general concepts applicable to all SQL databases.

You configure transaction isolation level through the code. It is part of the standard JDBC API. You could read about isolation levels in Oracle's tutorial here. That page also covers autocommit mode of the Connection.
Hi, John.

It's definitely not a socket problem. It is a problem related to threading. I think the root cause is a "non-atomic operation" which leads to a "race condition". You could also google about "lost updates". Lost updates are somewhat SQL-related, but they describes your problem quite well.

Consider the following scenario. Two clients tries to increase balance of account A on 10. Current balance is 3. Consider following sequence of events:

There are possible solutions. You could provide some set of locks to ensure that only one handler could be run at the time for each particular account. You could use JDBC transaction with a proper transaction isolation level (and without autocommit). You could write an "optimistic locking" solution (update balance if its value was not changed otherwise repeat an update code from the start). Or you could change your sql code to increment value instead of fetcing and then updating it. In this case I would prefer the latter approach (change an update statement to "increment value" statement). I could check for "update count" value if I need to perform some actions when update was not performed. For example, I could check if account exists and preconditions are OK to provide better error messages or log possible security issue.
Hi, Sam.

I suggest using an HTTP monitoring tool. For example, you may use a developer console in Firefox or Chrome. You could see an actual request made by the browser.

As to your problem. You are trying to use HTTP basic authentication. But the site passes login and password as form parameters in the POST request. It also passes other parameters which may or may not matter. You should match browser request as closely as possible first and then you could remove all non-necessary parts from it.
5 years ago
Hi, Satya

S Majumder wrote:Could you give a short example how to implement comapreTo method properly. Satya

You already have correct code commented out inside your implementation of compareTo. It is "ASC" sorting. Just uncomment it and comment your DESC section. I see that you was able to get items in ascending order in your last post. It is exactly what I was talking about.

If you need just specific order but not delays, you could use PriorityQueue. And for DelayQueue you have to have compareTo consistent with getDelay implementation (it is ascending order in your example). Otherwise DelayQueue's behaviour would be counterintuitive (it would wait longest timeout instead of shortest and this may change in future versions) and hard to debug.

I meant that you have to use take method to get items in order. Iterating through the queue using iterator would not give you a sorted sequence. Your screenshot shows that is is definitely not a desc order in the array. But this is a queue, it is not an array. Queue order is descending here. In other words, items would be taken out correctly. If you use take method instead iterator, you would see a sorted sequence.

I looked into your code once again. Check compareTo method in your first post. It is not consistent with the delay order (and actually it is an opposite order). It says that item with deleyTime=10 is before item with deleyTime=5. It should be opposite. Items with lesser delay should be less than items with greater delay. This would not give you a sorted sequence by itself. But fixing this along with using take method would give you items with the increasing deleyTime.

According getDelay function. It is not guaranteed when this function would be called. Current implementation calls this method inside take and poll methods. But this can be changed in future version. For example, this method could be called once when item is put into the queue and delay value would be stored.
Hi, Joe.

Monoid is type and operation which obeys monoid laws. Type alone do not define a monoid. For example, (Int, +) and (Int, *) are monoids for the Int type. Monoid rules define relationships between values and functions. They do not define an "identity function" itself. So it's not clear what do you mean when you say that rules could be represented by two functions.

Anyway, folds are not related to monoids. You could apply foldLeft operation to any collection of items and any operation. For monoid type and operation and for ordered collections (all these should apply at the same time) left and right folds would produce same result. But you can apply these folds for non-monoid operation on the type. For example, you could fold List[Int] using divide operation. So these operations are not specific (or related) to monoids at all.
5 years ago

Look at the line 48 in your code. Your Delayed instances are already expired because System.currentTimeMillis() returns quite large values. So all your items could be immediately taken out of the queue. Probably this is the reason why you see that output matches initial order.

Anyway, DelayQueue does not offer "iterate in sort order" guarantee. It only guarantees that items could not be taken before they expire. And do not expect to see sorted output even after providing positive delays. I think that DelayQueue uses heap data structure internally. It does not keeps items sorted. The only guarantee is that "you can extract items in order", but not "you can iterate items in order". PriorityQueue exhibits similar behaviour. Proper way to test the queue would be to provide positive delays and then extract items one by one.

P.S. Did you notice that in lines 48-49 you are dealing with milliseconds (currentTimeMillis) but pretend they are nanoseconds?