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
PleasantDilemma
Dec 5, 2006

The Last Hope for Peace

baka kaba posted:

Any chance that server is hosed up and is having its time changed constantly (like, say several times per second) between local time and UTC?

I haven't seen this. Everything is running locally on my workstation. But I just put it out on the dev environment and I can not reproduce the error. It must be something specific to my machine. I hope :ohdear:

Adbot
ADBOT LOVES YOU

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

PlesantDilemma posted:

I haven't seen this. Everything is running locally on my workstation. But I just put it out on the dev environment and I can not reproduce the error. It must be something specific to my machine. I hope :ohdear:

That is really odd. Can you rig it up so it will automatically log the results of the following query whenever you are doing queries?

code:
SELECT @@global.time_zone, @@session.time_zone;

Volmarias
Dec 31, 2002

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

PlesantDilemma posted:

I think I get the wrong raw results, but it must be a timezone related issue because the difference is always 7 hours. The thing that kills me is that it is not wrong every time. Here is a snip of my log files for 20 runs of the application. I just print the timestamp as soon as I get it as a string. I've filtered it to one row, #151281. On 4 of the runs the time was 11:05:41, but the time in the database is 03:05:41.

When you say you get the wrong raw results, are you querying for something that involves timestamps, or is other data wrong?

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

This might be a stupid question, but is there a design pattern for requiring classes to implement a static method? I have a bunch of classes like this:
code:
Strategy1
Strategy2
Strategy3
The idea is that they all perform some work on a data structure that's passed in, but each class does it differently - the code is potentially complex, which is why they're separated out to keep things neater, and easier to add to later. The usage in each case is the same - pass in the Map reference, and the data in it is manipulated. There's just one method, and I wanted to call it statically since there's no need for an instance of the class.

So to keep things straight, I wanted to enforce this pattern by using an interface or extending a class, so all of these classes are guaranteed to implement the static method I'm calling. I don't want to provide a default implementation, since that would allow new classes to avoid needing their own implementation, which is the whole point of these shenanigans. My initial thought is to make the method abstract in the interface/superclass, but it seems you can't define a method as abstract and static.

I get that they're sort of opposites, but the idea doesn't seem that strange to me - I want to force implementing classes to include a static method matching this signature, that's all. Is that even possible? It's not really necessary in this case, but I'm interested in the general idea of being able to force and require a certain pattern. If this is a terrible idea (or if I've not really explained myself) let me know!

This is on Android by the way, in case there's some fancy legendary developments in newer Java versions that I'll never see...

Gravity Pike
Feb 8, 2009

I find this discussion incredibly bland and disinteresting.

baka kaba posted:

I want to force implementing classes to include a static method matching this signature, that's all. Is that even possible?

There is no way to force a class to implement a static method. Having static "Utility Classes" is a bit of an anti-pattern in Java (even though everybody does it), and having a series of Utility Classes that all "implement an interface" is taking a bad idea a bit far.

Could you use instances of your classes, rather than having static methods? Do they need to store state? Could this be accomplished using the Singleton pattern?

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

Nah there's no state at all, that's why I'm making them static in the first place. They run a fixed procedure on the input and spit out a result (well, update the map). It's purely an organisational thing I'm trying, keeping them in their own classes. And singletons... I went there for something else, where I really shouldn't have in retrospect. Learned a lot though! Way more than I need for this though.

I mean I could create an instance - it's run rarely, so it doesn't really matter. And I could drop the whole template idea, since each class only has that one method anyway, so it would be hard to miss out! The main reason I started looking at it was so I could throw the Javadoc comment on the abstract version, so I wouldn't have to add it manually each time. Really this is total overkill for what I'm doing, but I like the idea of things all fitting together neatly, so I thought I'd look into it at least. I definitely have easy options I can take otherwise

Volguus
Mar 3, 2009

Gravity Pike posted:

There is no way to force a class to implement a static method. Having static "Utility Classes" is a bit of an anti-pattern in Java (even though everybody does it), and having a series of Utility Classes that all "implement an interface" is taking a bad idea a bit far.

Could you use instances of your classes, rather than having static methods? Do they need to store state? Could this be accomplished using the Singleton pattern?

Singleton ... not even once (just kidding, there are 2 valid uses of singleton).

Anyway, interface with static method? That's a wtf right there. Calling them a Strategy looks to me like you wanted to implement the strategy pattern: http://en.wikipedia.org/wiki/Strategy_pattern .

That is a valid pattern and there are very valid cases to implement that. Is your case valid? Only for you to decide.

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

rhag posted:

Singleton ... not even once (just kidding, there are 2 valid uses of singleton).

Anyway, interface with static method? That's a wtf right there. Calling them a Strategy looks to me like you wanted to implement the strategy pattern: http://en.wikipedia.org/wiki/Strategy_pattern .

That is a valid pattern and there are very valid cases to implement that. Is your case valid? Only for you to decide.

Thanks, that's really interesting - and it's funny that I came up with that pattern and named it the exact same way. Thank you serendipity

Yeah, that's effectively what I was going for, only my idea here is to use static classes and methods since there's "no actual need" to instantiate the class. I realise that might be completely wrong (if utility classes aren't meant to be a thing), and in reality in my case it's a single object used by another single object so who cares, but the idea doesn't seem weird to me. Basically just not creating objects if you can help it, and wanting to use an interface to define that static functionality in all the classes that implement it. Is this never a thing in any OO language?

Brain Candy
May 18, 2006

baka kaba posted:

Thanks, that's really interesting - and it's funny that I came up with that pattern and named it the exact same way. Thank you serendipity

Yeah, that's effectively what I was going for, only my idea here is to use static classes and methods since there's "no actual need" to instantiate the class. I realise that might be completely wrong (if utility classes aren't meant to be a thing), and in reality in my case it's a single object used by another single object so who cares, but the idea doesn't seem weird to me. Basically just not creating objects if you can help it, and wanting to use an interface to define that static functionality in all the classes that implement it. Is this never a thing in any OO language?

Not wanting to create objects is definitely premature optimization.

That said, static methods are not intrinsically bad. Utility classes are not automatically bad. See the perfectly fine Math or Collections "classes". Just don't let anybody put mutable state in there AND don't be afraid to change your mind if you start pushing in too many arguments.

Volguus
Mar 3, 2009

baka kaba posted:

Thanks, that's really interesting - and it's funny that I came up with that pattern and named it the exact same way. Thank you serendipity

Yeah, that's effectively what I was going for, only my idea here is to use static classes and methods since there's "no actual need" to instantiate the class. I realise that might be completely wrong (if utility classes aren't meant to be a thing), and in reality in my case it's a single object used by another single object so who cares, but the idea doesn't seem weird to me. Basically just not creating objects if you can help it, and wanting to use an interface to define that static functionality in all the classes that implement it. Is this never a thing in any OO language?

Don't worry about objects. The JVM will take care of them when they are not needed. Premature optimization is the root of all evil. Just do "the right thing" and optimize later or let the JIT to work for you (it's pure magic, i guarantee it). And, first and foremost, don't even try to optimize without measuring. Your gut feeling is most likely wrong in 99% of the cases.

Static methods may be fine but are extremely dangerous in a multithreaded environment (if the access/modify a static object at the same time, etc.). If you don't have a valid reason to use a static method, don't. a utility like "squareOfX(int x)" is fine, but much more than that odds are is not fine.

edit: wouldn't hurt to look at CDI libraries to help with strategy injection. Google guice if you're light or other more feature-full libraries (spring, jee, etc.) for when you're not.

Volguus fucked around with this message at 04:00 on Jun 14, 2014

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 don't worry, this isn't optimisation so much as trying to do things cleanly, and wondering if it's even possible or right. Like I said it's effectively one object called by another object, and it only happens during initialisations which are rare anyway, so I'm totally ok with instantiating whichever strategy class I'm using (and it's basically what I'll end up doing anyway). The irony is the strategies are handling some optimisation :haw:

I'll take a look at things like Guice, cheers - it's probably more than I need for what I'm doing here, but I've been meaning to get more familiar with useful libraries

-Blackadder-
Jan 2, 2007

Game....Blouses.
So I just completed the standard two course sequence: Intro to Computer Science w/ Java. I'm currently wondering about is where I should go from here in terms of personal study?

I learned a lot in those first two classes, but reading the net I'm coming away with the feeling that some of what we were doing, while important for foundational understanding, is kind of archaic in modern times. Just as a broad general example I'm reading that there's a lot of focus on mobile/web development these days. I saw someone say on another Java forum that they haven't written a desktop application in 10 years, it's just been servlets and JSP.

Perhaps it's even time for me to try out other languages? What's hot/useful these days that I should be boning up on? I'm hearing talk about Scala being the "next big thing". What are peoples thoughts on that?

Basically the intro classes were great and helpful but more than anything they made me realize that my world is pretty small in terms of coding knowledge and the coding scene in general. So I'm looking for personal study advice on expanding my knowledge and awareness.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Are you doing this for a degree, or did you just take those two courses? If it's the former, I'd say don't worry so much about breadth, just read around the threads here or other places to get an idea of what is out there. Yeah it's true that web and mobile are the big areas now, but desktop software isn't going away just yet. Also, Scala is actually a functional language, which is a very different paradigm from object-oriented programming, so you may find it wonderful and the best ever, or incredibly confusing. Definitely try it, or Haskell, to see what it's like, but don't worry too much if it seems like you're over your head.

I'd say look around at Android/iOS/WinPhone or web development. They're in demand, but they also have (usually) decent tooling available, depending on what computer you use (you'll need Windows for WinPhone and OS X for iOS, but Android and web can be done pretty much anywhere.) However, I will say that you can generally pick up new languages pretty quickly once you become a better programmer, and that if you have an idea for an application, feel free to do it in Java even if it's not an "in" language. You'll learn a lot going through the whole process of development and refinement, and learning how to use the specific libraries you need to get things done.

What I would recommend you do in any case is to open a Github account and learn how to use Git. Then whatever projects you do you can save on there and, if/when you go looking for jobs, can clean up the projects and have a portfolio of work to show off. It's very useful, even outside of the benefits that source control has for development.

carry on then fucked around with this message at 03:03 on Jun 15, 2014

Gravity Pike
Feb 8, 2009

I find this discussion incredibly bland and disinteresting.

-Blackadder- posted:

So I just completed the standard two course sequence: Intro to Computer Science w/ Java. I'm currently wondering about is where I should go from here in terms of personal study?

I mean, desktop applications aren't really a huge thing anymore; everything that can be done on a web page is. That being said, those webpages have to connect to web servers; these servers are primarily written in Java, C#, and Ruby on Rails.

Java is still a great language to be proficient in. It is used everywhere; it is big, old, stable, and fast. There are already libraries written to do basically everything, so you don't need to worry about reimplementing a loading-cache, you can just use Google's. If you have a problem, you can paste your exception into google, and someone's probably already solved it on Stack Overflow.

I've taken a few Scala classes over on Coursera. My team considered using it, but ultimately decided against it. The tooling is kind of crap around it (it's hard to debug), and the syntax is a little too loose - there are enough ways to do something that you end up with each library being a different DSL, so there's always a learning curve in the way of understanding what's going on. We took away some of the lessons of Scala (make everything immutable, within reason), and kept on using Java.

In order to be a Good Developer, you should be familiar with a compiled language, a scripting language, bash scripting, and a version control system (like git or subversion). I'd recommend tooling around with Python a bit if you haven't; it is also very widely used.

The very best thing you can do to prepare for industry is start contributing to an open-source project. The biggest complaint that I have against developers just-out-of-school is that they don't know how to work with others, and they don't know how to contribute within an existing project. Everything you do in school tends to be written on your own from scratch. I've hired someone almost exclusively on the strength of their GitHub profile; they flubbed the coding interview, but I could see that they were a competent developer.

-Blackadder-
Jan 2, 2007

Game....Blouses.

carry on then posted:

Are you doing this for a degree, or did you just take those two courses?

Yeah, I'm pursuing a bachelor's in Computer Science but I've got a quarter or two of other non-cs classes before I transfer from my CC to university so I wanted stay busy and keep improving my skills.

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
A sort of theoretical question:

I want to implement a sort of list of updates, potentially stretching to an indifinite size, so it would be undesirable to load it all at once like a Java object. I suspect the way to do it is with a database. I've found a simple tutorial, but I'm daunted by the fact that it appears that the database server has to be started outside of Java, and doesn't appear very portable if run locally - this program I'm making is for personal use and I shuttle it between Linux and Windows via Dropbox. So I'd like to know if working with a database would involve many shenanigans. If so, maybe I could do with only recent updates as a short list.

Volguus
Mar 3, 2009

supermikhail posted:

A sort of theoretical question:

I want to implement a sort of list of updates, potentially stretching to an indifinite size, so it would be undesirable to load it all at once like a Java object. I suspect the way to do it is with a database. I've found a simple tutorial, but I'm daunted by the fact that it appears that the database server has to be started outside of Java, and doesn't appear very portable if run locally - this program I'm making is for personal use and I shuttle it between Linux and Windows via Dropbox. So I'd like to know if working with a database would involve many shenanigans. If so, maybe I could do with only recent updates as a short list.

Use H2 as the database. There is no server, just a file, but it behaves just like a normal database. Then perform your "pagination" using normal SQL (with LIMIT and friends).

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
Thanks, that looks very promising.

PleasantDilemma
Dec 5, 2006

The Last Hope for Peace

fletcher posted:

That is really odd. Can you rig it up so it will automatically log the results of the following query whenever you are doing queries?

SQL code:
SELECT @@global.time_zone, @@session.time_zone;

I added this and my results are always "+00:00" for both. So MySQL is in UTC.

Volmarias posted:

When you say you get the wrong raw results, are you querying for something that involves timestamps, or is other data wrong?

The only incorrect data I am seeing is when I pull from a MySQL TIMESTAMP column.

Thanks for the helpful responses guys! I just figured out the problem today. I was not correctly setting Javas timezone to UTC. I had this in my code at the top of the part that does all my data processing:

Java code:
TimeZone UTC = TimeZone.getTimeZone("UTC");
TimeZone.setDefault(UTC);
And I thought that was good enough. I think the problem is that by the time this code is executed a few database connections have been made with the existing default Pacific timezone. Then the rest of the code executes and more database connections are added to the connection pool. This means I have some connections in UTC time and some in pacific time, hence why I was not always getting the timezone I expected.

I moved my code that sets default timezone to the top of the entire app and now all my results are as expected. Also, I added -Duser.timezone=UTC to how the jar in invoked just to be super explicit because gently caress timezones. Man this was really frustrating.

0zzyRocks
Jul 10, 2001

Lord of the broken bong

PlesantDilemma posted:

Also, I added -Duser.timezone=UTC to how the jar in invoked just to be super explicit because gently caress timezones. Man this was really frustrating.

Just echoing this sentiment - gently caress timezones. Glad you found a simple solution.

PleasantDilemma
Dec 5, 2006

The Last Hope for Peace
I need a human readable file format for a little tool I'm making. I need to describe a series of messages. I could use json but I'd like something more human friendly if it is out there.

I'm trying to follow the examples at snakeyaml.org for YAML but I'm not understanding how to use the Constructor and TypeDescription system. This is the structure I need.

Java can deal with .properties files without any extra libraries. Are there other file formats it can deal with natively?

Gravity Pike
Feb 8, 2009

I find this discussion incredibly bland and disinteresting.

PlesantDilemma posted:

I need a human readable file format for a little tool I'm making. I need to describe a series of messages. I could use json but I'd like something more human friendly if it is out there.

I'm trying to follow the examples at snakeyaml.org for YAML but I'm not understanding how to use the Constructor and TypeDescription system. This is the structure I need.

Java can deal with .properties files without any extra libraries. Are there other file formats it can deal with natively?

Personally, I have a strong preference for JSON unless there is a compelling reason not to. The Gson library is super nice to work with.

code:

class Config {
  public int deviceId;
  public String backendHost;
  public int backendPort;
}

class Packet {
  public int typeId;
  public int objectId;
  public int asn1;
  public Date date;
  public double lat;
  public double lng;
}

class MyInput {
  public Config config;
  public List<Packet> packet;
}

Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
MyInput input = gson.fromJson(aString, MyInput.class);
Also works with bean-style objects (setters+getters), but I didn't feel like typing all of those out.

Volguus
Mar 3, 2009

PlesantDilemma posted:

I need a human readable file format for a little tool I'm making. I need to describe a series of messages. I could use json but I'd like something more human friendly if it is out there.

I'm trying to follow the examples at snakeyaml.org for YAML but I'm not understanding how to use the Constructor and TypeDescription system. This is the structure I need.

Java can deal with .properties files without any extra libraries. Are there other file formats it can deal with natively?

If you have a flat key-value structure, going with anything else than properties file is probably over-complicating things. If you have an object tree, JSON,XML and YAML will do fine (Java can read XML natively, needs 3rd party libraries for the other two).
However, from my experience with YAML, it is super awkward to work with (both the library and the file format). The format itself tends to be quite loose, therefore harder to control, in my opinion.

The JSON libraries that I've tried (Gson and Jackson) are quite nice and easy to work with. XML gets a bad rap because the format is verbose, and some people say that the library (native one) is hard to work with, though I personally don't agree.

As for your question, apparently all you need to do it create a MyProperties file that has a Config member and a List<Packet>. Config and Packet have their appropriate members that match those properties. From their examples (https://code.google.com/p/snakeyaml/source/browse/src/test/java/org/yaml/snakeyaml/Example2_27Test.java) this should be enough.

edit: Now thinking about it, you did say a human friendly format. I assume that the reason you want a human friendly format is that a human will enter the data. Now, the most user-friendly way for them to enter the data is for you to provide them with an app to do so, at which point the file format is irrelevant. If that's not possible, take a look at excel (or its ODF counterpart). There are java libraries to read from excel and odds are your users are familiar with it anyhow. It won't be that easy to read from it as it is from XML or JSON, but ... they'll be happy.

Volguus fucked around with this message at 13:30 on Jun 26, 2014

Chas McGill
Oct 29, 2010

loves Fat Philippe
Got a question about composition vs inheritance.

Example:
An outdoor centre has bikes and boats available for rent.

Bike:
make
model
idNumber
ageGroup

Boat:
make
model
idNumber
capacity

Those are just examples of attributes the classes might have.

My inclination would be to have an abstract Vehicle class to capture their commonalities, but I've recently been learning about composition and also see its benefits. However, inheritance seems to 'make more sense' to me here since there's such an obvious is-a relationship. A Boat is a vehicle.

On the other hand, I feel like I'm probably ascribing too much real world meaning to the system. It feels weird to say that a boat references an instance of vehicle, rather than just being a vehicle. But it doesn't seem as weird to say that the role of Teacher has-a Person who fulfils it.

PleasantDilemma
Dec 5, 2006

The Last Hope for Peace
Thanks for the feedback guys!

Gravity Pike posted:

Personally, I have a strong preference for JSON unless there is a compelling reason not to. The Gson library is super nice to work with.

Wow that is simple. I'll probably go with this.

rhag posted:

Now thinking about it, you did say a human friendly format. I assume that the reason you want a human friendly format is that a human will enter the data. Now, the most user-friendly way for them to enter the data is for you to provide them with an app to do so, at which point the file format is irrelevant.

I'm building an internal tool for QA and other engineers to use, so I don't feel it's too much for them to write out a file. Also, it makes it easier for documentation, they can just attach the input file to the bug if needed.

FateFree
Nov 14, 2003

Chas McGill posted:

Got a question about composition vs inheritance.


In your example inheritance seems like a fine solution. But you shouldn't think of composition as a boat referencing an instance of a vehicle. The boat is still a vehicle, it just maybe internally has an object that helps store some of its state like some sort of Model(make, model, id) class.

The value of composition comes from exposing partial functionality, or for example using dependency injection to easily swap out the guts of a class without anyone knowing about it.

Gravity Pike
Feb 8, 2009

I find this discussion incredibly bland and disinteresting.

Chas McGill posted:

Got a question about composition vs inheritance.
:words:

I mean, the right answer is "whatever makes your code work." Do you have a situation where you have something that could be a boat or a bicycle, and you want to deal with whatever-it-is as a vehicle? Or do you have a Person, and want them to act as a Doctor in one situation, and a Mother in another? If you're not in either of these situations, gently caress it, don't bother making unnecessary hierarchical structures for the sake of having a hierarchy. It isn't terribly important that your code is an accurate description of The World, because you're not trying to model the world; you're usually trying to solve a very specific problem.

Gravity Pike fucked around with this message at 08:07 on Jun 27, 2014

pigdog
Apr 23, 2004

by Smythe
It would be okay to use inheritance in this case, though most of the time, you use inheritance if you wish to inherit behavior, the code that's in the parent class.

In this case I'd probably have a RentableItem interface which includes all the stuff common to all rentable items that are needed to offer and rent an item (eg name, id, price, offer or pricelist reference, stuff like that) from the rental bookkeeping perspective, and leave the rest to evolve as you go for the time being. Interfaces are cool because they don't give dusty fucks about inheritance, any class can implement them, so you could as well have Tent or Boots classes with all kinds of different fields, but as long as they implement the methods dictated by the interface, from the sales perspective they're rentable assets all the same.

Chas McGill
Oct 29, 2010

loves Fat Philippe

FateFree posted:

In your example inheritance seems like a fine solution. But you shouldn't think of composition as a boat referencing an instance of a vehicle. The boat is still a vehicle, it just maybe internally has an object that helps store some of its state like some sort of Model(make, model, id) class.

The value of composition comes from exposing partial functionality, or for example using dependency injection to easily swap out the guts of a class without anyone knowing about it.

Gravity Pike posted:

I mean, the right answer is "whatever makes your code work." Do you have a situation where you have something that could be a boat or a bicycle, and you want to deal with whatever-it-is as a vehicle? Or do you have a Person, and want them to act as a Doctor in one situation, and a Mother in another? If you're not in either of these situations, gently caress it, don't bother making unnecessary hierarchical structures for the sake of having a hierarchy. It isn't terribly important that your code is an accurate description of The World, because you're not trying to model the world; you're usually trying to solve a very specific problem.

Thanks for the answers. I find structural stuff like this interesting, as well as the psychology of how the way I think code should work reflects how I think the world works, even if that doesn't necessarily translate to an efficient solution. It's tough to separate what a class symbolises to me and how it should function.

Going to read about dependency injection etc now.

pigdog posted:

It would be okay to use inheritance in this case, though most of the time, you use inheritance if you wish to inherit behavior, the code that's in the parent class.

In this case I'd probably have a RentableItem interface which includes all the stuff common to all rentable items that are needed to offer and rent an item (eg name, id, price, offer or pricelist reference, stuff like that) from the rental bookkeeping perspective, and leave the rest to evolve as you go for the time being. Interfaces are cool because they don't give dusty fucks about inheritance, any class can implement them, so you could as well have Tent or Boots classes with all kinds of different fields, but as long as they implement the methods dictated by the interface, from the sales perspective they're rentable assets all the same.
Yeah, I see how that would work as well. Composition and inheritance came up in the context of avoiding code duplication (in the course I'm doing) but having an interface seems like a more flexible solution for this kind of project.

Chas McGill fucked around with this message at 14:09 on Jun 27, 2014

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...
+1 to interfaces (and possibly composition and factories) for making your code clean.

Oh god what did I just write :gonk:

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

Java code:
Volmarias implements InterfaceFan

more like dICK
Feb 15, 2010

This is inevitable.
Simple Spring question (I hope!). I'm using JavaConfig for the first time, and I've got a couple of beans like this:

Java code:
    @Bean
    public ContextSource contextSource() {
        LdapContextSource contextSource = new LdapContextSource();
        contextSource.setBase(env.getProperty("ldap.base"));
        contextSource.setUserDn(env.getProperty("ldap.dn"));
        contextSource.setPassword(env.getProperty("ldap.password"));
        contextSource.setUrl(env.getProperty("ldap.url"));
        return contextSource;
    }

    @Bean
    public LdapTemplate ldapTemplate() {
        LdapTemplate ldapTemplate = new LdapTemplate();
        ldapTemplate.setContextSource(contextSource());
        return ldapTemplate;
    }
The LdapTemplate gets @Autowired into another class elsewhere, while the ContextSource is only ever used here in the configuration file as a property of the LdapTemplate.

Do I need to manually call afterPropertiesSet on either of these? Why or why not?

FateFree
Nov 14, 2003

more like dICK posted:

Do I need to manually call afterPropertiesSet on either of these? Why or why not?

I can't say for sure offhand but why don't you put a debug breakpoint in the afterPropertiesSet method of one of the beans and see if the spring container calls it automatically or not?

geeves
Sep 16, 2004

Gravity Pike posted:

Personally, I have a strong preference for JSON unless there is a compelling reason not to. The Gson library is super nice to work with.

code:

class Config {
  public int deviceId;
  public String backendHost;
  public int backendPort;
}

class Packet {
  public int typeId;
  public int objectId;
  public int asn1;
  public Date date;
  public double lat;
  public double lng;
}

class MyInput {
  public Config config;
  public List<Packet> packet;
}

Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
MyInput input = gson.fromJson(aString, MyInput.class);
Also works with bean-style objects (setters+getters), but I didn't feel like typing all of those out.

Gson is great. Just be weary if you use with with Hibernate objects (specifically 3.x, haven't used 4.x) as it can get messy with mapped objects that could end up circularly referenced. So make sure you use the @Expose annotation in those cases.

TheresaJayne
Jul 1, 2011

more like dICK posted:



Do I need to manually call afterPropertiesSet on either of these? Why or why not?

I did a tutorial post a while back for Spring standalone maybe that will help....

http://girlcoderuk.wordpress.com/2013/07/14/configuring-hibernate-and-spring-for-standalone-application/

more like dICK
Feb 15, 2010

This is inevitable.

FateFree posted:

I can't say for sure offhand but why don't you put a debug breakpoint in the afterPropertiesSet method of one of the beans and see if the spring container calls it automatically or not?

I'm not thinking right today, I guess. Debugging third party libraries in Java is much easier than what I'm used to with C++. The container calls afterPropertiesSet. Looks like the bean is actually constructed before I ever call the annotated method. Direct calls to the method get proxied.

Chas McGill
Oct 29, 2010

loves Fat Philippe
Thick question incoming:

How do I check if a collection only contains a certain value or object? I know about aCollection.contains(whatever) but I'm not sure how I'd make sure the collection doesn't contain anything else. I've been googling but haven't found anything useful.

(This is for a test method)

baquerd
Jul 2, 2007

by FactsAreUseless

Chas McGill posted:

Thick question incoming:

How do I check if a collection only contains a certain value or object? I know about aCollection.contains(whatever) but I'm not sure how I'd make sure the collection doesn't contain anything else. I've been googling but haven't found anything useful.

(This is for a test method)

code:
for (Object o : aCollection) {
  if (o == null || !o.equals(whatever)) {
    return false
  }
}
return true;

Chas McGill
Oct 29, 2010

loves Fat Philippe

baquerd posted:

code:
for (Object o : aCollection) {
  if (o == null || !o.equals(whatever)) {
    return false
  }
}
return true;

Ugh what a dope I am. Thanks.

Adbot
ADBOT LOVES YOU

Newf
Feb 14, 2006
I appreciate hacky sack on a much deeper level than you.
return aCollection.contains(anObject) && aCollection.size() == 1; ?

Newf fucked around with this message at 14:43 on Jul 2, 2014

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