Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!

Stephan van Hulst

Saloon Keeper
+ Follow
since Sep 20, 2010
Enschede, The Netherlands
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 Stephan van Hulst

Welcome to CodeRanch!

It's a bad idea to read from and write to a file at the same time. Many I/O utilities buffer their input and output before the data is actually returned or written, and having different utilities do it at the same time almost always leads to unexpected results.

You need to use try-with-resources to properly dispose of your readers and writers, and you need to make sure that the scope of those blocks don't overlap for the same resources. This is what your program would look like, if you really want to read a number from the same file after writing to it once:

I'm not sure why you want to do this though. Maybe you can explain some more about your application.
18 hours ago
Who says that? Do you have sources for that statement? I've never head the term "partially checked exceptions" before.
Thanks Tim, food for thought. One thing:

Tim Holloway wrote:POJO means "Plain Old Java Object". Once you cannot arbitrarily substitute one bean for another, you've locked yourself in. You now have coupling issues and code reuse limitations. That doesn't mean that you cannot inject complex objects into a POJO, just that it means that you must not require a class of POJO objects to accept a specific property or properties, since then it's by definition no longer a POJO.

I still don't understand what you mean by this. Why isn't this a POJO:
1 day ago

Rob Bank wrote:While I think I can come up with regexes to modify the input data to exclude 1., and ,+0.00.00, I cannot rearrange the different fields.

I haven't actually looked at your raw HTML input yet, or the way in which you process it. I will do so later and see if I can come up with something useful for you.

Specifically in your latest Participant class from line 21 Participant(...) { to line 114 return new Participant() where there are a number of blocks with different orders on the Participant fields, does any of the blocks require the input data to be 1.41.22, A, Mickelsson, Mick, New York?

The parse() and scanDuration() methods depend on the ordering of the fields. What blocks don't you understand? If you quote them, I will try to explain what they do and why.

Note that if you are using an IDE such as NetBeans or Eclipse, you can set breakpoints in the code and step through it one statement at a time, and see what the intermediate results are. This is a very educational process, and will also help you understand and debug other programs.
1 day ago

Michael D Sims wrote:See I never did get full comprehension of ExecutorService ... but maybe I should look at it again

An ExecutorService is simply an interface for objects that responsible for executing tasks. People tend to say that they want start a thread to do something, but that's putting the means ahead of the goal. It's like saying you want to use a hammer to install some rivets. No. You want to install rivets. Does it really matter that you want to use a hammer or a rivet gun? In this analogy, a thread is the hammer, and a task is the rivet.

Here's the outline I would use for the server code:

I will say this about Java ... for being a pretty fun language to code in, I'm NOT impressed with how difficult it is doing most things GUI...

For most people, GUI coding isn't a lot of fun regardless of the language you use. Personally, I enjoy writing GUIs in Swing, but I think using an imperative language to describe a user interface is inherently flawed. I believe JavaFX takes a more declarative approach, but I haven't worked with it before and I don't know how it "feels". These things tend to become more interesting when you have some experience with it.

So these new tasks that are wrapped up in a nice Task Factory / ExecutorService package could also be useful for passing information back and forth as needed?

Back and forth between what?
All of this is very reasonable. Using threads directly is a bit old-fashioned, however. Let your main thread create an ExecutorService, which you pass to the class that's responsible for listening for incoming connections. It starts listening by submitting a task to the executor service that handles the server socket. Every time a new connection comes in, that task creates a new task to handle the connection, and submits it to the executor service. That way, the (number of) tasks that need to be performed is completely decoupled from the (number of) threads that perform them.

It would be extra nice of you don't just pass an ExecutorService to your server class, but also a task factory, that creates new tasks that handle your incoming connections. That way your server class will work not just for chat applications, but for any application that has multiple clients.
You still haven't explained what the parameter of Storable means. It just makes no sense to me.
4 days ago
Sorry for hijacking the topic. After this post, I'll leave it at that. Or I'll just split the topic.

I will take your points about me Putting My Money Where My Mouth Is, and about the fact that programming At Home is not the same as programming In Real Life. I am certain one never needs static variables in fresh code, but I'll have to consider how to fix existing applications in a cost-effective manner.

I have some final questions. Why is it important to maintain insertion order? I agree that for things that absolutely need to be processed in insertion order (database migrations or whatever), an increasing sequence number is the natural key. But then the ID has semantic meaning. I advocate GUIDs for records that don't have a meaningful combination of properties that make them unique. And how does an increasing sequence number help in slicing the database, when after many deletions over time, there could be many large gaps between successive records?

Can you explain what you mean by that POJOs are not supposed to have infrastructure-related property mandates? Does that mean that I can't simply pass a logger(-factory) in a constructor?
4 days ago

Rob Bank wrote:Can I check if you are in C# mode? Seems var is not a Java syntax.

Haha, yes. I use C# a lot at work and sometimes I hammer out code snippets without checking them in my compiler. I apologize.

And RaceResult parse does not accept path so do you mean;

RaceResult has an overload that accepts a path, which reads the lines from a CSV file, and then delegates parsing those lines to the overload that accepts a stream of lines.

This does not printf anything but neither do I get any direct errors so I'm thinking this could work once I figure out how to correctly implement some modifications I did to Participant to more accurately match my app objectives.

Are you sure that the file you're reading from is not empty, and that the output is directed to where you expect it to be?

Lightbulb, I think I get the intension of path in RaceResult. It enables a main shortcut like;

Yes. This is what I was going for in my previous post, but goofed up.
5 days ago

Tim Holloway wrote:1b. GUIDs are HORRIBLE database keys. They're effectively long random strings, so you cannot slice a database according to any logical function. Which can interfere with optimization and housekeeping.

Why do you want to perform such a logical function on an ID? Either the property that uniquely identifies a record is meaningful, in which can you use neither auto-increment nor a GUID, or the ID isn't meaningful, and you wouldn't perform logic on it.

They have no inherent sequence, so you'd better have at least one other unique chronometric field in the table if you want to be able to access by order added.

Okay. That's fine isn't it?

Use GUIDs as, say, invoice ID numbers and see how customers react.

This is a good argument. I don't have an elegant solution to this. I guess for invoices, I would generate an ID by mangling the properties that make the invoice unique, or use a database that DOES generate identifiers correctly.

The one thing GUIDs are good for is (virtually) ensuring that every record in the table has a truly Globally Unique ID, regardless of where it was originally created. And that's a great help when you have a system with multiple feeds, but not for most other applications.

I would argue that it's not the responsibility of the application to know this, but of the service that it uses to store data. Regardless of whether you use a GUID or something else, I don't think you should generate it statically from your application.

2. Who passes around random number generators? Unless you are specifically looking for repeatability of "random" numbers on a per-thread basis in a multi-threaded environment, one RNG is all most people need, and there's no need to instance it.

I do, in most of my classes that need a source of randomness. Greatly improves my ability to unit test the application as well.

3. See Item #2. It's even less likely that you want multiple instances of a generator for a blockchain. [...] Having more than one generator defeats the whole concept.

I don't agree that needing only one instance of an object automatically means it should be static. Singleton classes cause so much bugs in large applications. People use them because they can't be bothered to properly declare dependencies between objects. That's just lazy programming.

Which reminds me. The most popular logging classes also use static accessors to obtain the logger instance. Because, again, that avoids the need to explicity inject or construct them.

Really, what's the problem with just injecting them explicitly?
5 days ago

Rob Bank wrote:Those concepts aside, I'm still stuck on the usage of lineOfCsv in the Participant class and path in the RaceResult class and how they relate.

As I see it, RaceResult invokes Participant participant. There is no invoking the other way around. So, after I in my main class do Participant.parse(lineOfCsv), RaceResult can operate on my data parsed into Participant. Hence, what is the intention of the RaceResult class reading path?

That's just a convenience method that reads lines of CSV from a file, and converts them to a RaceResult. If you don't want to read CSV from a file, but want to provide them from a different source, you would call the parse(Stream<String> lines) method directly.

What seems most logical to me (still not knowing what to do with RaceResult's path) would be to (in my main class) do [...] where I get errors like Cannot make a static reference to the non-static method participants() from the type RaceResult

That's because you call participants() on the RaceResult class, but you need to call it on an object of type RaceResult. Given that you have a file with comma separated values, and the path name is stored in the pathResult variable, this is how the main method would look:
5 days ago
Alternatively, if you want to maintain the type of the elemental unit, you can add another type parameter.
5 days ago
I don't understand why you have a generic type parameter on Storable. What does it mean? What if I make a class WaterBottle implements Storable<Number>?

If you remove it, you have the following:
5 days ago
That's just the message that the JVM prints out. 'Exception' probably just reads nicer than 'throwable'.

Yes, you should distinguish between the two, even if the message doesn't. Throwable includes critical errors that you probably shouldn't catch.
6 days ago