Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Ola
Jul 19, 2004

Thanks for the tips! Please do pile on with the abstractions, that's exactly what our professors do. The chess project is over, so I won't be working more on that, but it was interesting to see how little you can get away with in representing two players and the state of the game. We stored FEN strings in the database and the remote player could then fetch that and draw the new board.

I can't really see that it would ever grow into 1000s of different queries, but another project might, I'll cross that bridge when I get there.

For now though, let's say we have a ConnectionPool, a DBManager and for the sake of simplicity, a MainProgram which does everything else. Are there good reasons for the MainProgram to inject a connection than for the DBManager to "inject itself" by fetching one?

I.e.

code:
public class MainProgram {
    ...
    DBManager.store(ConnectionPool.getConnection(),data1,data2);
    ...
}

public static class DBManager{
    public static void store(Connection connection, String data1, int data2){
    //do things
    }
}
vs


code:
public class MainProgram {
    ...
    DBManager.store(data1,data2);
    ...
}

public static class DBManager{
    public static void store(String data1, int data2){
    Connection connection = ConnectionPool.getConnection()
    //do things
    }
}
Just answered my own question while typing that out, for testing so I can inject test db credentials instead of production ones. :) Any other reasons?

e: and thinking some more, it's pretty obvious that you might want to use different dbs from time to time, maybe based on load, location or which particular function is in use.

Ola fucked around with this message at 10:05 on May 21, 2018

Adbot
ADBOT LOVES YOU

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

It depends on what MainProgram's responsibilities are. Should it be responsible for acquiring Connections and passing them around every time something needs to use one? If so that's fine! But if that's more of an internal consideration for DBManager, it probably makes more sense to keep the connection stuff in there, so you're not mixing logic through a bunch of different classes

But you're still calling a static method, on the ConnectionPool class. That's why calling Connection Pool.getConnection inside the DBManager is bad - you've hardwired this link between two classes, they're inseparable. So if you wanted to do all the connection management inside DBManager (so you can just call #store without having to go get a connection to pass in) you'd typically pass in an instance of ConnectionPool, usually when you're constructing the DBManager, and just call #getConnection on that whenever you need one

That way you've injected the component it depends on, and it can happily make use of that to ask for connections or whatever. But because you've supplied that object, you're in full control of what it is and what it does - so it's not tied to a particular implementation like when you use static stuff. And of course, you can create a singleton ConnectionPool if that's what you need (say if multiple components all need to use the same pool), you just inject that same instance into everything that has it as a dependency. (This is another thing DI frameworks can manage for you, defining singletons and scopes to automatically create or reuse a thing, which can be handy on more complex projects)

So yeah, it's ok for your dependency manager (the thing that's handing out the components) to call static getters and factory methods to get the instances it'll hand out, because it's like the top level, the source, the thing that creates the dependencies instead of being given them. Everything else should be given instances and interact with those specific objects. So you're ok doing ConnectionPool.getConnection() in MainProgram if you're getting Connections to hand out, because that class is acting as your dependency manager, the source of all these things. It's typically easier to stick all that in a separate class(es) though, just so it's all in one place, like a recipe collection

I hope that's not too confusing :ohdear:

baka kaba fucked around with this message at 18:01 on May 21, 2018

Ola
Jul 19, 2004

baka kaba posted:

I hope that's not too confusing :ohdear:

No, that was good, thanks! The idea is ConnectionPool was a singleton and the DBManager static, because the DBManager (a gradually worse name the fewer responsibilities it has) is just SQL boilerplate, set up PreparedStatement, pull stuff out of ResultSet, catch exceptions and return the result. But feeding it different connections means you could query different databases or with different users.

One more question if you don't mind, let's say you have a big complex MainProgram (across multiple classes ofc), it might be some business tool that does everything from billing and orders to customer relations to market analysis etc, with the data stored in SQL. That's a whole bunch of different queries. Would you code those queries (the actual string, not the PreparedStatement) in each MainProgram method that calls the DBManager, or would the DBManager have hundreds of ready-made queries in methods like DBManager.addNewEmployee, or would there be some SQL query repository class serving as the manager for that particular dependency?

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...
.

Volguus
Mar 3, 2009

Ola posted:

No, that was good, thanks! The idea is ConnectionPool was a singleton and the DBManager static, because the DBManager (a gradually worse name the fewer responsibilities it has) is just SQL boilerplate, set up PreparedStatement, pull stuff out of ResultSet, catch exceptions and return the result. But feeding it different connections means you could query different databases or with different users.

One more question if you don't mind, let's say you have a big complex MainProgram (across multiple classes ofc), it might be some business tool that does everything from billing and orders to customer relations to market analysis etc, with the data stored in SQL. That's a whole bunch of different queries. Would you code those queries (the actual string, not the PreparedStatement) in each MainProgram method that calls the DBManager, or would the DBManager have hundreds of ready-made queries in methods like DBManager.addNewEmployee, or would there be some SQL query repository class serving as the manager for that particular dependency?

If you really insist on having your DBManager with static methods, then it should make its own PreparedStatements and textual SQL queries and only provide methods like "addNewEmployee". But, as I said before, this DBManager can get really big really fast. It will handle employees, and billing, and orders and customers. Too much, in my opinion. EmployeeRepository.save() would be more approrpiate.

The DBManager approach is ok when you are very sure that it will stay small as the application is part of is very small. Then no, you don't need to go crazy with repositories and services and whatnot. By small I mean under 500 lines.

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Yeah, that's more of an organisational thing. You don't want DBManager becoming some kind of god object with a method for every single "do thing that touches database" operation your entire application performs, and you also don't really want your MainProgram to duplicate every one of those methods, just so it can pass it along internally by calling the DBManager version

So it's cool when DBManager is small and you only have a handful of database operations. When it gets bigger it makes more sense to separate out those concerns, so you have a different 'manager' for things like accounts, sales etc. That's where the repository pattern Volguus is talking about is useful - you get an object focused on a specific domain, with only the methods that relate to that

It's neat and easy to reason with and change, but it also keeps components separate. Accounts and Employees probably aren't closely related to each other, being able to treat them separately makes it possible to switch one out - for load balancing, using a different data store, testing etc. Having to replace the entire DBManager to change some parts of it is, well... I'm sure you get the idea at this point!

Ola
Jul 19, 2004

Volguus posted:

If you really insist on having your DBManager with static methods, then it should make its own PreparedStatements and textual SQL queries and only provide methods like "addNewEmployee". But, as I said before, this DBManager can get really big really fast. It will handle employees, and billing, and orders and customers. Too much, in my opinion. EmployeeRepository.save() would be more approrpiate.

The DBManager approach is ok when you are very sure that it will stay small as the application is part of is very small. Then no, you don't need to go crazy with repositories and services and whatnot. By small I mean under 500 lines.

Yeah I see that. So for all the different categories of things you could do, something with employees, something with customers etc, they'd implement their own .save() method. I guess I'll need to look into better ways of building the actual queries as well.

Joe Desperado
Mar 11, 2008
What's the recommended IDE these days? Took a look at the OP and good god this thread is like a decade old. I was just wanting to mess around with libgdx so something quick and easy would be nice.

Pedestrian Xing
Jul 19, 2007

I like NetBeans, I think most people use IntelliJ.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

Joe Desperado posted:

What's the recommended IDE these days? Took a look at the OP and good god this thread is like a decade old. I was just wanting to mess around with libgdx so something quick and easy would be nice.

I don't think the IDE recommendations for Java have changed much in the last 10 years :D

I use IntelliJ daily, it is a wonderful IDE. I can't speak to anything specifically about libgdx though. Looks like they have some info though: https://github.com/libgdx/libgdx/wiki/Gradle-and-Intellij-IDEA

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Another recommendation for IntelliJ. Sometimes it feels like it's the only one with continuing user interface improvements. Has NetBeans had any releases after being transferred to Apache?

Joe Desperado
Mar 11, 2008
Cool. I used to use NetBeans waaay back in the day and haven't touched Java in years.

A lot of recs for IntelliJ though so I'll definitely check that out.

Volguus
Mar 3, 2009
IntelliJ has a free version, so you can definitely try it out. If you need the pro features, netbeans and eclipse are better. Simply you can't beat the value proposition. Unless you try them all though, you can't make an informed decision (and sometimes that's fine, to fool around you don't need much).

Zaphod42
Sep 13, 2012

If there's anything more important than my ego around, I want it caught and shot now.

Joe Desperado posted:

What's the recommended IDE these days? Took a look at the OP and good god this thread is like a decade old. I was just wanting to mess around with libgdx so something quick and easy would be nice.

Eclipse or IntelliJ

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...

Volguus posted:

IntelliJ has a free version, so you can definitely try it out. If you need the pro features, netbeans and eclipse are better. Simply you can't beat the value proposition. Unless you try them all though, you can't make an informed decision (and sometimes that's fine, to fool around you don't need much).

I used eclipse for many years before finally coming over to IntelliJ and good gravy is it better and nicer. What are you seeing with Eclipse that IntelliJ doesn't offer?

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Volmarias posted:

I used eclipse for many years before finally coming over to IntelliJ and good gravy is it better and nicer. What are you seeing with Eclipse that IntelliJ doesn't offer?

For me, Java EE support, in the community edition.

underage at the vape shop
May 11, 2011

by Cyrano4747
Why does this go over the file twice? (I know what I'm doing with the constructor call, this is just getting it working)
code:
public class SalesLogParser {
	Stock stock=new Stock();
	
	
	public SalesLogParser() throws FileNotFoundException {
		parse("docs/ASSDOCS/sales_log_1.csv");
	}
	
	public void parse(String fileName) throws FileNotFoundException {
		File file =new File(fileName);
		Scanner input = new Scanner(file);
		input.useDelimiter(",");
		ArrayList<String> lines = new ArrayList<String>();
		
		while(input.hasNextLine()) {
			String data=input.nextLine();
			System.out.println(data);
		
		}
		input.close();
	
	}
}
E: The CSV I am reading is a list of item names and how many are sold, like
rice,20
frozen vegetables,40

Is it seeing the comma as meaning theres a new line? The output in the console is correct, apart from being double.

underage at the vape shop fucked around with this message at 13:29 on May 23, 2018

Volguus
Mar 3, 2009

underage at the vape shop posted:

Why does this go over the file twice? (I know what I'm doing with the constructor call, this is just getting it working)


It doesn't. It goes through the file only once.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

underage at the vape shop posted:

Why does this go over the file twice? (I know what I'm doing with the constructor call, this is just getting it working)
code:
public class SalesLogParser {
	Stock stock=new Stock();
	
	
	public SalesLogParser() throws FileNotFoundException {
		parse("docs/ASSDOCS/sales_log_1.csv");
	}
	
	public void parse(String fileName) throws FileNotFoundException {
		File file =new File(fileName);
		Scanner input = new Scanner(file);
		input.useDelimiter(",");
		ArrayList<String> lines = new ArrayList<String>();
		
		while(input.hasNextLine()) {
			String data=input.nextLine();
			System.out.println(data);
		
		}
		input.close();
	
	}
}
E: The CSV I am reading is a list of item names and how many are sold, like
rice,20
frozen vegetables,40

Is it seeing the comma as meaning theres a new line? The output in the console is correct, apart from being double.

It doesn't do that for me. This doesn't seem like it's all the code, so I would say search for another call to parse() in your project. I assume you've already checked the CSV and it isn't doubled in the input?

underage at the vape shop
May 11, 2011

by Cyrano4747
aaaaaa its because of someone else mistake in a unit test. they fixed it.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
So, since Java serialization is being removed at some point (because it is involved in a very large number of java vulnerabilities), what options do I have for type safe (and easy) serialization to disk for my burp and fart hobby app?

Volguus
Mar 3, 2009

Wheany posted:

So, since Java serialization is being removed at some point (because it is involved in a very large number of java vulnerabilities), what options do I have for type safe (and easy) serialization to disk for my burp and fart hobby app?

Text formats: xml, json, properties
Binary formats: protobuf, msgpack, fast buffers

Comedy option: make your own
If you really hate yourself: yaml

Keetron
Sep 26, 2008

Check out my enormous testicles in my TFLC log!

Small question.

I have some database from which I pull all the data from this one table (13K records but 150K on disk, some fields have HUGE content) and put it in a List.
In the implementation, I need to search this List a zillion times.
I am worried this is slow, due to the size, it will kill the nightly batchjob.

Should I ramp up the serverspecs this thing is running on or should I be a good boy and just query the server every time?

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...
Does the table data fit into memory if you fetch it all at once? If not, do you HAVE to fetch all of the columns of the table data, or can you fetch most and get the rest as needed?

Can your searches map to the list in some way? Would having a Map help?

Can your zillion queries be optimized in some way?

Pedestrian Xing
Jul 19, 2007

That sounds like something you should let the DB handle as long as it's indexed properly. If it has to be done in memory, make some kind of Map structure so you're not searching over and over. Also possibly Streams if it doesn't have to be done in a specific order?

Keetron
Sep 26, 2008

Check out my enormous testicles in my TFLC log!

Yeah, gonna take the column I search on and the one I need, put it in a Map and go from there.

Pedestrian Xing
Jul 19, 2007

Trip report: Mac pretty good so far, having a lot harder time adjusting to IntelliJ from Netbeans. My macros... :qq:

poemdexter
Feb 18, 2005

Hooray Indie Games!

College Slice

Pedestrian Xing posted:

Trip report: Mac pretty good so far, having a lot harder time adjusting to IntelliJ from Netbeans. My macros... :qq:

Recommended by another goon a long time ago, but go get Key Promoter X plugin. It will tell you the hotkeys for things that you do often so you can pick them up quickly.

PierreTheMime
Dec 9, 2004

Hero of hormagaunts everywhere!
Buglord
Anyone familiar enough with file encoding to tell me why the hell Apache CSVParser is giving me unexpected output?

I'm creating a reader, setting the expected file format to UTF-8, and applying that to the CSVParser:
code:
Reader reader = Files.newBufferedReader(Paths.get(filepath), StandardCharsets.UTF_8);
csvParser = new CSVParser(reader, CSVFormat.newFormat(delimiter.charAt(0)).withHeader().withFirstRecordAsHeader().withIgnoreHeaderCase().withTrim());
When I pass the map keysets (header column names) and check each column name against the "master" expected headers, the first column gets kicked out because it's converting the BOM to "?" instead of "" as I'd expect:
code:
private void printDiscards() {
	//Get all existing header values and check against "correct" values, adding mismatches to list to output
	Enumeration<String> headerValues = Collections.enumeration(csvParser.getHeaderMap().keySet());
	while (headerValues.hasMoreElements()) {
		boolean columnFound = false;
		String headerValue = (String) headerValues.nextElement();
		for (String column : correctColumns) {
			if (column.equalsIgnoreCase(headerValue)) {
				columnFound=true;
			}
		}
		if (!columnFound) {
			discardedColumns.add(headerValue);
		}
		}
	for (String discardedColumn : discardedColumns) {
		System.out.println("Column name " + discardedColumn + " does not match expected pattern and will be discarded!");
	}
}
This code works fine if you provide your file as ANSI format, but if you give it UTF-8 as it's set up to expect, gives me the following:

Column name ?Client_Product_ID does not match expected pattern and will be discarded!

Edit: Apparently I hadn't Googled for the correct phrase up until now. Looks like its a bug that Apache made a utility for: BOMInputStream. Since I have a very specific application for this, I'll probably just do a replace on the string if its the first column String.Nevermind, I've rewritten the input stream to include the commons fix. Blarg.

PierreTheMime fucked around with this message at 18:10 on Jun 18, 2018

Pedestrian Xing
Jul 19, 2007

Java Readers intentionally ignore the existence of a BOM in a string/file which is annoying. Commons-io has a BOMInputStream that will deal with it.

oh no computer
May 27, 2003

I haven't seriously coded in Java for about 10 years, but I'm looking for a new job in the next few months so I'd like to get back into it in case any Java Dev jobs come up (that I'll still probably get rejected for due to the aforementioned lack of recent experience).

I still have a couple of Java programming books I could use to refresh my memory, but they are for Java 5, and a cursory Google shows that Java is now on version 10. Has the language changed enough that it would be worth me looking into using some more up-to-date learning materials, or would I be alright re-learning from the books I have and then just learning about the more recent changes afterwards?

Zaphod42
Sep 13, 2012

If there's anything more important than my ego around, I want it caught and shot now.
Most of the changes from version to version are pretty minor, good java is still about the same as it was.

That said, read up on lambdas and streams that we got in 8 or 9, super useful. (Although not really anything you couldn't do another way. But so much cleaner) also we have method references now, which goes with lambdas.

Also maybe read up on some dependency injection framework.

CPColin
Sep 9, 2003

Big ol' smile.
Yeah, your Java 5 book probably won't teach you too many bad habits. When you learn about streams and lambdas in Java 8, remember to consider readability and clarity as you construct monster, multi-step stream operations and you'll be fine.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Read about Java Modules if you're curious why everyone seems to be staying on Java 8 instead of moving up.

brap
Aug 23, 2004

Grimey Drawer
Java 9 modules feels like such a disappointment to me. The only reason I gave a poo poo is I thought it would provide a better solution to classpath hell by letting you mark certain dependencies as internal and not visible to consumers of your package. But no such luck.

baquerd
Jul 2, 2007

by FactsAreUseless

brap posted:

Java 9 modules feels like such a disappointment to me. The only reason I gave a poo poo is I thought it would provide a better solution to classpath hell by letting you mark certain dependencies as internal and not visible to consumers of your package. But no such luck.

Yep, that was going to be sweet instead of dumb.

TheresaJayne
Jul 1, 2011
Most, Companies are still using Java 6 or 7 its rare to find one on Java 8 even,

John F Bennett
Jan 30, 2013

I always wear my wedding ring. It's my trademark.

And at the same time, almost every job offer description I've seen over here requires knowledge of Java 8.

(And AngularJS, but that's a different matter)

venutolo
Jun 4, 2003

Dinosaur Gum

TheresaJayne posted:

Most, Companies are still using Java 6 or 7 its rare to find one on Java 8 even,

What makes you say this?

Data from Jet Brains's 2018 survey, Rebel Labs' 2017 report, and Plumbr's 2017 data all indicate that Java 8+ is in high use. This also matches with my experience at the local JUG whenever a speaker asks what version of Java we are using. Obviously, these all can be subject to self-selection bias, but I'd be interested if there is contradicting data.

Adbot
ADBOT LOVES YOU

CPColin
Sep 9, 2003

Big ol' smile.
I can't switch some of the web applications I've inherited to Java 8 because they're on Grails 2 and need to be upgraded to Grails 3 before they can handle Java 8. :suicide: (I'm planning to upgrade them to "not Grails or Groovy anything," instead.)

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply