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
baquerd
Jul 2, 2007

by FactsAreUseless

Newf posted:

return aCollection.contains(anObject) && aCollection.size() == 1; ?

Depends on whether copies are allowed.

Adbot
ADBOT LOVES YOU

Chas McGill
Oct 29, 2010

loves Fat Philippe
Either work for my purposes. Thanks. Having brain problems today. It's just for a test to make sure a collection only contains a specific object after a method is invoked.

FateFree
Nov 14, 2003

Newf posted:

return aCollection.contains(anObject) && aCollection.size() == 1; ?

Its funny but I can't help look at this and say the size check should be first for performance reasons to short out the contains. Do you guys consider stuff like this to be premature optimization? In my view, as long as you aren't doing 'extra' work under the pretense of optimization, then I would just consider swapping to be the objectively correct action.

Max Facetime
Apr 18, 2009

Newf posted:

return aCollection.contains(anObject) && aCollection.size() == 1; ?

return Arrays.asList(anObject).equals(aCollection);

;)

Newf
Feb 14, 2006
I appreciate hacky sack on a much deeper level than you.

FateFree posted:

Its funny but I can't help look at this and say the size check should be first for performance reasons to short out the contains. Do you guys consider stuff like this to be premature optimization? In my view, as long as you aren't doing 'extra' work under the pretense of optimization, then I would just consider swapping to be the objectively correct action.

All work is extra work!

As for whether this is premature optimization - it's a little bit contextual. Is there a possibility in this system of this collection being terribly large? It's likely that even the five seconds of typing to swap the order of those checks is longer than the cumulative performance hit of the inefficiency over the entire lifetime of the system.

In general, I tend to build first and optimize when I'm given some indication that there would be a derived benefit.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe

FateFree posted:

Its funny but I can't help look at this and say the size check should be first for performance reasons to short out the contains. Do you guys consider stuff like this to be premature optimization? In my view, as long as you aren't doing 'extra' work under the pretense of optimization, then I would just consider swapping to be the objectively correct action.

You are assuming that the size check is faster than the contains which isn't necessarily true for all collections.
ConcurrentLinkedQueue

FateFree
Nov 14, 2003

Janitor Prime posted:

You are assuming that the size check is faster than the contains which isn't necessarily true for all collections.
ConcurrentLinkedQueue

Hah, I guess baquerd's solution was the best after all! Although now we're in a situation where I would consider it premature to choose that over the one liner we were talking about, just to have less code. Interesting.

Sedro
Dec 31, 2008

baquerd posted:

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

Fails for
[ 1, 1 ] contains 1
[ null ] contains null

Java code:
static boolean containsOnly(Iterable coll, Object o) {
    Iterator i = coll.iterator();
    return i.hasNext() && Objects.equals(i.next(), o) && !i.hasNext();
}
Edit: it depends if you want to allow duplicates, I was going off the other solutions

Sedro fucked around with this message at 19:21 on Jul 3, 2014

Helpimscared
Jun 16, 2014

Can someone tell me why Java was designed to be so verbose?

I'm learning it as my first(ish) language and compared to the python I've done its just a pain in the rear end. Maybe its better for bigger projects but Java has discouraged me from wanting to learn programing.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

It was designed to be C++ but safer (at least syntax and semantics wise.) Other than that, the reason for it is usually something along the lines of "because it's a statically typed object-oriented programming language."

Gravity Pike
Feb 8, 2009

I find this discussion incredibly bland and disinteresting.

Helpimscared posted:

Can someone tell me why Java was designed to be so verbose?

I'm learning it as my first(ish) language and compared to the python I've done its just a pain in the rear end. Maybe its better for bigger projects but Java has discouraged me from wanting to learn programing.

It was partially a reaction to C/C++, which tended to be very concise, but also difficult to read. It's also an artifact of the age of the language; Java is 20 years old, and Java 1.1 (source and bytecode) is still valid up to Java 1.8. (Conversely, there are significant breaking changes between Python 2 & 3.)

I dunno, Java's just kind of a wordy language. Get an IDE with auto-completion. Nobody actually types all of that boiler-plate out. You just bang on CTRL+SPACE until your program writes itself.

pigdog
Apr 23, 2004

by Smythe

Gravity Pike posted:

I dunno, Java's just kind of a wordy language. Get an IDE with auto-completion. Nobody actually types all of that boiler-plate out. You just bang on CTRL+SPACE until your program writes itself.
This. The language is wordy, but it allows for very good tools. Although I guess Python IDEs can be pretty decent, too.

22 Eargesplitten
Oct 10, 2010



I used netbeans to set up a jFrame gui, is there any way to unprotect the code that it auto generated? I want to set the text based off of variables, and I don't see support for that in the design view.

edit: While I'm here, I created an instance of a class in the constructor for the jFrame, and now when I try to reference that instance from an event I get the message cannot find symbol.

Relevant code:
code:
public PA13GUI() {
        initComponents();
        Overview Status= new Overview();
    }

 private void ImpBarrActionPerformed(java.awt.event.ActionEvent evt) {                                        
    Status.setMoney(Status.getMoney()-Status.getImpBarrCost());
    Status.setImpBarrCost(Status.GetImpBarrCost()*1.25);//
    }

22 Eargesplitten fucked around with this message at 11:38 on Jul 5, 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

What if you make Status lowercase? It looks like you're trying to access a Status class statically at the bottom there

22 Eargesplitten
Oct 10, 2010



Just tried that, still had the exact same problem.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Status is scoped to the constructor, and falls out of scope the moment the constructor is done. Make an instance variable and assign to that, and it'll stick around.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
When you declare a variable inside a method (including a constructor), the name you give to the variable only means something inside that method body. Anywhere else, the name doesn't exist.

What you want to do is declare your variable as a member variable of the class, and fill it in inside the constructor - by making it a member variable, the name you give to it is valid anywhere inside the class, so you can refer to it in your other methods.

Have you worked your way through the Java Tutorials yet? In particular, the page on Declaring Member Variables is relevant here.

22 Eargesplitten
Oct 10, 2010



I'm currently working on the swing tutorials. I thought I still remembered enough of my 3 semesters of java to skip the beginning ones, but apparently not. Declaring the variables outside of the constructor and initializing them in the constructor did the trick.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe

Jabor posted:

What you want to do is declare your variable as a member variable of the class, and fill it in inside the constructor - by making it a member variable, the name you give to it is valid anywhere inside the class, so you can refer to it in your other methods.

Except for static methods inside the class :eng101:

Helpimscared
Jun 16, 2014

After learning Java more, I think i like it more than Python. So i take back complaining about its verbosity.

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

Are you at the point yet where you've stopped saying 'what the gently caress is this unreadable poo poo' about

Java code:
Cat cat = new Cat();
and started doing it intentionally yourself, because you think it's a good idea?

Chas McGill
Oct 29, 2010

loves Fat Philippe
Java is my first programming language (not counting the Duplo that is Scratch) and I don't mind its verbosity either. I'd rather be able to read through a process on 4 lines and understand it than have it on 1 line made up of abbreviations and obscure symbols. I like the structure that the syntax forces at the moment. Maybe when I'm more experienced I'll chafe against it though.

Zaphod42
Sep 13, 2012

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

22 Eargesplitten posted:

I'm currently working on the swing tutorials. I thought I still remembered enough of my 3 semesters of java to skip the beginning ones, but apparently not. Declaring the variables outside of the constructor and initializing them in the constructor did the trick.

The fundamentals are pretty much everything in programming. It is extremely tempting to skip ahead to "doing stuff", lots of people do it, but you risk just copy and pasting code as a crutch and asking for help without understanding it. If you're learning from the questions that's more or less fine, but be careful you don't go too far in over your head, I've seen lots of people do it and then get burned out fast. Swing isn't exactly the most straightforward of libraries to use, if you're not super familiar with java.


Helpimscared posted:

Can someone tell me why Java was designed to be so verbose?

I'm learning it as my first(ish) language and compared to the python I've done its just a pain in the rear end. Maybe its better for bigger projects but Java has discouraged me from wanting to learn programing.

Python is pretty clean, although C programmers would still call it rather verbose.
Its all kinda relative dude. Compared to Ruby, Java is rather concise and obscure :cheeky:

Languages aren't be-all-end-all. They're each different tools in the programmer's toolkit. Learning a new language, if you really grok programming, doesn't take very long. Use the right one for the right job. You've got dozens of verbose languages and not to pick from, and we'd be the worse if we only had one way or the other.

Gravity Pike
Feb 8, 2009

I find this discussion incredibly bland and disinteresting.
I'm having a bit of a maven dependency issue. I've got a project with a few submodules, one of which is depended on by the others.

pom.xm
code:
<groupId>org.gravitypike</groupId>
<artifactId>my-service</artifactId>
<version>1.0-SNAPSHOT</version>
<name>my-service</name>

<modules>
  <modules>my-service-interface</modules>
  <module>my-service-implementation</modules>
</modules>

<dependencyManagement>
  <dependencies>
    <dependency>
      <gropuId>org.gravitypike</groupId>
      <artifactId>my-service-interface</groupId>
      <version>${project.version}</version>
    </dependency>
  </dependencies>
</dependency>
my-service-implementation/pom.xml
code:
<groupId>org.gravitypike</groupId>
<artifactId>my-service-implementation</artifactId>
<version>1.0-SNAPSHOT</version>

<parent>
  <groupId>org.gravitypike</groupId>
  <artifactId>my-service</artifactId>
  <version>1.0-SNAPSHOT</version>
</parent>

<dependencies>
  <dependency>
    <groupId>org.gravitypike</groupId>
    <artifactId>my-service-interface</artifactId>
  </dependency>
</dependency>
If I do mvn clean install, everything's peachy. However, my build server runs mvn dependency:resolve first, and the build fails, because it can't find the snapshot version of my interface. Am I doing something horribly wrong with my dependency management here, or should I just figure out how to configure my build server not to run this check?

Volmarias
Dec 31, 2002

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

baka kaba posted:

Are you at the point yet where you've stopped saying 'what the gently caress is this unreadable poo poo' about

Java code:
Cat cat = new Cat();
and started doing it intentionally yourself, because you think it's a good idea?

Creating... a new instance? I'm not sure I understand the criticism?

Cryolite
Oct 2, 2006
sodium aluminum fluoride
I think it's because cat is repeated 3 times.

Volmarias
Dec 31, 2002

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

Cryolite posted:

I think it's because cat is repeated 3 times.

Java code:
Animal belovedPet = new PetFactory().setStrategy(new FelineStrategy()).get();
:v:

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 when I first started I wasn't used to the general type = new constructor() style, with the same class name referenced on both sides. Giving the variable the same name on top of that just seemed like a huge 'gently caress you' to clarity and comprehension, and these were tutorials

http://en.wikipedia.org/wiki/Buffalo_buffalo_Buffalo_buffalo_buffalo_buffalo_Buffalo_buffalo

Anyway of course now I do it myself when I don't need a descriptive name :shobon:

Cryolite
Oct 2, 2006
sodium aluminum fluoride

Volmarias posted:

Java code:
Animal belovedPet = new PetFactory().setStrategy(new FelineStrategy()).get();
:v:

Ah, but you should be injecting those dependencies!

Volmarias
Dec 31, 2002

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

Cryolite posted:

Ah, but you should be injecting those dependencies!

Who are you, my vet?

HFX
Nov 29, 2004

Gravity Pike posted:

I'm having a bit of a maven dependency issue. I've got a project with a few submodules, one of which is depended on by the others.

pom.xm
code:
<groupId>org.gravitypike</groupId>
<artifactId>my-service</artifactId>
<version>1.0-SNAPSHOT</version>
<name>my-service</name>

<modules>
  <modules>my-service-interface</modules>
  <module>my-service-implementation</modules>
</modules>

<dependencyManagement>
  <dependencies>
    <dependency>
      <gropuId>org.gravitypike</groupId>
      <artifactId>my-service-interface</groupId>
      <version>${project.version}</version>
    </dependency>
  </dependencies>
</dependency>
my-service-implementation/pom.xml
code:
<groupId>org.gravitypike</groupId>
<artifactId>my-service-implementation</artifactId>
<version>1.0-SNAPSHOT</version>

<parent>
  <groupId>org.gravitypike</groupId>
  <artifactId>my-service</artifactId>
  <version>1.0-SNAPSHOT</version>
</parent>

<dependencies>
  <dependency>
    <groupId>org.gravitypike</groupId>
    <artifactId>my-service-interface</artifactId>
  </dependency>
</dependency>
If I do mvn clean install, everything's peachy. However, my build server runs mvn dependency:resolve first, and the build fails, because it can't find the snapshot version of my interface. Am I doing something horribly wrong with my dependency management here, or should I just figure out how to configure my build server not to run this check?

The problem is it is declared in the parent pom file which means it will resolve first even if not used by the submodules. I've never found a good way to deal with this problem other then explicitly declare the dependancy everywhere.

You could explicitly bind the dependancy:resolve target to a later stage, but that would be very dangerous.

Could you perhaps move the interface into its own build project?

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe
The reason it works on your machine is because you've probably done a clean install of the interface manually at some point in the past and so maven has it installed in it's local repo. So when you try and build the parent pom it will find it, but if you were to wipe your local maven repo and try and do a clean install it would obviously fail since it has no notion of the interface.

Sedro
Dec 31, 2008

Gravity Pike posted:

If I do mvn clean install, everything's peachy. However, my build server runs mvn dependency:resolve first, and the build fails, because it can't find the snapshot version of my interface. Am I doing something horribly wrong with my dependency management here, or should I just figure out how to configure my build server not to run this check?
dependency:resolve resolves from the repository, so of course you would have to install first. You need to fix your build server.

Gravity Pike
Feb 8, 2009

I find this discussion incredibly bland and disinteresting.

Janitor Prime posted:

The reason it works on your machine is because you've probably done a clean install of the interface manually at some point in the past and so maven has it installed in it's local repo. So when you try and build the parent pom it will find it, but if you were to wipe your local maven repo and try and do a clean install it would obviously fail since it has no notion of the interface.

Nah, I made sure to blow away my ~/.m2/cache/org/gravitypike folder first (or whatever it is.) Basically, if I mvn release:prepare && mvn release:perform on localhost, my remote build server can't figure out where the newest snapshot interface is; if I mvn release:prepare && mvn release:perform && mvn clean deploy on localhost, I build the newest snapshot successfully, upload it to my artifact server, and then my build server is able to figure things out from there.

Sedro posted:

dependency:resolve resolves from the repository, so of course you would have to install first. You need to fix your build server.

Probably something along these lines. Is dependency:resolve the correct thing to be doing pre-build, or should I just try to deploy, and welp if I fail mid-build because of an unresolvable dependency?


For reference, I just joined a brand new babby startup, and we're using CircleCI instead of maintaining our own build-server with Jenkins or Hudson or whatever. Can't say enough good things about those dudes; they have been super responsive to basically every issue, and pretty transparent about their timeline for features we want that they do not yet have. They are lacking some pretty important features at the moment (building a library doesn't kick off builds of down-stream projects automatically), but seem a good deal while we don't really have the manpower to hire a full-time build/operations engineer.

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...
CloudBees is Jenkins in the cloud, something to think about.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
Let's say you've got a large suite of unit tests, but they were unfortunately written in a way where order of execution is important (why oh why). They take a long time to run so parallel-izing them looks attractive, how do you even begin to unravel the madness of the execution order?

Volguus
Mar 3, 2009

fletcher posted:

Let's say you've got a large suite of unit tests, but they were unfortunately written in a way where order of execution is important (why oh why). They take a long time to run so parallel-izing them looks attractive, how do you even begin to unravel the madness of the execution order?

Apparently TestNG can: http://stackoverflow.com/questions/2669576/order-of-execution-of-tests-in-testng
Another possible way could be to have test methods that run other methods in whatever order they want. Those other methods would not be marked as test. Of course, if you have special JUnit annotations (@Before, @After, @RunWith, etc.) that those "tests" require ... good luck.
I know that it's probably a thing that has to be done by yesterday, but I would strongly consider refactoring/rewriting them so that they can be run independently. It will bite you in the butt later on, and it's not gonna be pretty.

Kenishi
Nov 18, 2010
I've been working some Java alg exercises on paper and I've just had a brainfart.

code:
// Node are single linkedlist nodes
// Assume less and cur are valid.
Node temp = cur.next;
cur.next = less.next;
less.next = cur;
cur = temp;
'cur' will equal cur.next after this all executes. And less.next will still be the old cur value. Correct?

emanresu tnuocca
Sep 2, 2011

by Athanatos
Hey, I got this assignment to create a primitive 'to-do list' manager with a text based menu and all sort of junk, there's a certain hierarchy where I must have 'task category' which may contain 'tasks' which may contain 'subtasks', the entire application is contained in a 'todolist' type.

So, due to the way I want my text menu class to work I decided that defining two abstract classes would greatly simplify things, so I created a TodoListElement abstract class and a TodoListContainer abstract class which extends the TodoListElement class.

One of the fields in the 'TodoListElement' class is 'TodoListContainer parentContainer' which basically points one step higher in the hierarchy towards the container this element is contained within. Now, obviously the highest level in the hierarchy which is 'todolist' by definition shouldn't actually have a parentContainer, I am not exactly sure how I can override this field and its setters\getters given that the implementation within the abstract superclass 'todolistelement' is not abstract.

Can I simply declare the field 'parentContainer' (and the setter/getter) as final (= 'null') within the 'todolist' class, will it correctly hide the super classes' field and related methods?

Adbot
ADBOT LOVES YOU

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

emanresu tnuocca posted:

Hey, I got this assignment to create a primitive 'to-do list' manager with a text based menu and all sort of junk, there's a certain hierarchy where I must have 'task category' which may contain 'tasks' which may contain 'subtasks', the entire application is contained in a 'todolist' type.

So, due to the way I want my text menu class to work I decided that defining two abstract classes would greatly simplify things, so I created a TodoListElement abstract class and a TodoListContainer abstract class which extends the TodoListElement class.

One of the fields in the 'TodoListElement' class is 'TodoListContainer parentContainer' which basically points one step higher in the hierarchy towards the container this element is contained within. Now, obviously the highest level in the hierarchy which is 'todolist' by definition shouldn't actually have a parentContainer, I am not exactly sure how I can override this field and its setters\getters given that the implementation within the abstract superclass 'todolistelement' is not abstract.

Can I simply declare the field 'parentContainer' (and the setter/getter) as final (= 'null') within the 'todolist' class, will it correctly hide the super classes' field and related methods?

You shouldn't need to mess with the field - simply overriding the getter to always return null, and the setter to do nothing, should be sufficient.

On the other hand, what's the harm if someone does add your top-level container as a child of some other container? Is disallowing this really what you want to do?

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