|
How do i generate a random long from 1-7 billion, when there is no range function like there is with integer?
|
# ¿ Nov 22, 2009 15:25 |
|
|
# ¿ Apr 27, 2024 13:19 |
|
Yes thats handy for ints, but I can't get a number over 2 billion that way. There is no corresponding long(long range) method in random, i imagine because the int version uses a long to catch overflows. So I guess something special has to be done for my case but I'm not sure what it is.
|
# ¿ Nov 22, 2009 16:45 |
|
Parantumaton posted:If your desired range is 1...7 billion and you can get to 2 billion, why not just do that one to three times, add those three together and then add another billion? Well the chances are completely different. To hit 7,000,000,000 exactly should allow 1/7,000,000,000. Your way requires 1/2,000,000,000 three times in a row, which is (1/2,000,000,000) ^ 3 which is astoundingly higher
|
# ¿ Nov 22, 2009 19:09 |
|
Psychorider posted:The same way you do it if you don't have a range function for integer seems to work. Hmm i see where the confusion is, i didn't mean from 1 billion to 7 billion, just the old school 1 to 7 billion. So should this be the correct approach? code:
|
# ¿ Nov 23, 2009 00:05 |
|
LakeMalcom posted:For the random number/long guy: how about this: http://java.sun.com/j2se/1.4.2/docs/api/java/math/BigInteger.html#BigInteger(int,%20java.util.Random) Thanks, I have taken this approach and am stumped by a simple math problem. So 2^33 is the lowest power greater than 7billion, so I can use new BigInteger(33, rand) and discard anything over my range. My question is how I can determine the number 33 dynamically. In math terms, how do I find the smallest power of 2 thats greater than a given number, in fast java code?
|
# ¿ Nov 24, 2009 17:15 |
|
I am in posted:Also http://java.sun.com/j2se/1.4.2/docs/api/java/math/BigInteger.html#bitLength%28%29 Ah thanks that seems to work perfectly. Heres what I ended up with: code:
|
# ¿ Nov 24, 2009 20:20 |
|
Mulloy posted:
I'm surprised it even compiles. What is engine referring to? I dont see an instance variable. But anyway, assuming there is one, are you instantiating aGrid anywhere? you set it statically but its not set to anything, so by default its null.
|
# ¿ Nov 24, 2009 20:36 |
|
I have an architecture question that surfaced after a huge refactoring of my web app framework (thanks to ignoring generics for so long). I have a GenericDao<T> class to retrieve objects from the database with type safety. Standard methods like save(T), List<T> findAll(), etc. I want to add a new method that adds restrictions to queries to enable things like finding all Users by username. The method signature would look something like List<T> findBy(String field, Object property). However I want to offer some protection and not actually accept Strings and objects, but something more type safe. For instance, I should only allow the GenericDao<User> to call findBy like so: findBy("username", String), where "username" is a fixed value in the form of a constant or even better, an Enum. I can define an Enum of acceptable values but I run into an inheritance problem because I want all of my objects to be able to accept the common properties such as Long id, Long timeCreated, Long timeModified. As I understand I can't extend an Enum class which has some common values. Can anyone give me some insight to how the best way to architect this may be? Even if its not a solution, some direction might help clarify things for me.
|
# ¿ Jan 5, 2010 02:12 |
|
I am a reflection noob. I have a class with holds serveral static IProperty interfaces, and some static IReference interfaces. If I pass an object into a utility method, could I use reflection to get the instances of all these interfaces, without knowing their property names, so I could do a for(IProperty property : properties) loop?
|
# ¿ Jan 11, 2010 16:33 |
|
Question about generics and inheritance. I have a domain model hierarchy like so: Persistable > Address Persistable as a parent has fields that all persistable objects have, like id, timeCreated, timeModified. Address is a subclass which has properties such as address, city, state, yada yada. I have a Dao class which allows for dynamic where clauses. Traditionally, I just used static strings to identify columns, like so: code:
code:
code:
code:
FateFree fucked around with this message at 00:21 on Jan 26, 2010 |
# ¿ Jan 25, 2010 23:09 |
|
rjmccall posted:An excellent idea! Surely your work is touched with genius. Well thanks, I tried to use the super property but apparently I wasn't using it correctly. Your way solved my problem. Was there something wrong with my original Property<T, V> idea in general for the sarcastic genius remark?
|
# ¿ Jan 26, 2010 00:02 |
|
rjmccall posted:Nope; it's just that it's pretty much exactly what I told you to do twenty days ago when you asked this question then. At touche salesman. I editted my last question, since I asked on a couple forums I didn't remember that I asked here. But works great now, thank you.
|
# ¿ Jan 26, 2010 00:23 |
|
I'm bored and want to try something a little different. I want to code some people objects, and more importantly try to code their thought process as close as possible to the real thing, which I might expand into a game at a later point but is really just a POC at this point. I'd like to have a virtual room full of them and basically watch them interact. This is far from what I normally code so my question is how to best architect this. The first things that come to mind are should each person run in their own thread, should I be looking into the Executor class for that? Can anyone give a high level overview or any resources so I can start with the implementation?
|
# ¿ Feb 23, 2010 14:42 |
|
Im using reflection for a Service toString. As an example, AccountService has a field UserService, which is an interface implemented by UserServiceImpl and its parent is AbstractServiceImpl. Assuming I already have the Field object for the UserService, how can I determine if it is an implementation of AbstractService? For example if I print out field.getType, I see the interface UserService. But if I try to do AbstractServiceImpl.class.isAssignableFrom(field.getType()), it always returns false. Sorry for the convoluted question but it gets down to what condition can I use so that any Field that extends AbstractServiceImpl returns true?
|
# ¿ Jun 10, 2010 04:57 |
|
If I'm making a generic class <T extends BaseObject, V> how can I enforce that V is a long? If I say <V extends Long> I get an ugly warning about not being able to extend final classes, even though it seems to accomplish what I want. Is there any other way?
|
# ¿ Jun 16, 2010 17:45 |
|
Yes this turned out to be the answer. I got caught up in generics insanity before I realized it. Thanks. Now for something a little harder. I am using EhCache (thread safe cache, based on ConcurrentHashMap) to store a counter (AtomicInteger). The counter is basically the count of all rows in the database for some query. I'd like to pro-actively manage this value to reduce the number of queries, so I have code like so, that I will call any time someone saves a new object to the database: code:
My question is, is this thread safe? I know the AtomicInteger is a synchronized increment operation, and I know that ehcache is thread safe, but I don't know if this particular block of code is safe to use together. I think it is because of AtomicInteger, and if it was a regular int with counter++ it would be unsafe. Still I'd feel better with someone elses opinion.
|
# ¿ Jun 16, 2010 23:03 |
|
Well lets see, there may not be a count in cache yet, and I would not have the information to add one. All I know is on a new save, its count++ and a delete its count--, the actual number requires a query (whoever is requesting the count will first check cache, if not there, queury and place it in cache). But if another thread inserted something after my get, it means they will be putting the most up to date number in cache since its coming directly from the query. So i believe in that sense it is safe.
|
# ¿ Jun 17, 2010 00:35 |
|
Just when you think youve got something figured out haha. Thank you sir. I suppose the best solution is to maintain the count in cache, and just clear it on save or delete operations. I guess there is no way to ever guarantee an accurate count, even if it was never cached another thread could have saved a value immediately after I retrieved mine.
|
# ¿ Jun 17, 2010 02:27 |
|
tef posted:http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ConcurrentHashMap.html#putIfAbsent(K, V) Unfortunately while the idea of the putIfAbsent is nice, the only way it could ever be effective is if you passed some sort of interface that will create the value, rather than sending the actual value. Imagine if the value came from an expensive database call, you would only want to make that call if it is in fact absent from the cache, but this method would require you to send the value every time. I'm hoping one day they improve that..
|
# ¿ Jun 17, 2010 13:18 |
|
Can anyone explain why I cant seem to get the following code to work with try finally blocks? This is for a gzip compression algorithm: Compress: code:
code:
Here was one such attempt: code:
code:
|
# ¿ Jun 27, 2010 23:31 |
|
Thanks for the explanation, I appreciate it. With that in mind, is it possible for me to consolidate my input and output streams since they seem to just be wrapping themselves a few times, and call close on only the parent stream? For instance: This code:
code:
|
# ¿ Jun 28, 2010 01:47 |
|
Sorry to bring this back but I have a concurrency question about using ConcurrentHashMap. I've voiced my complaints in the past about the putIfAbsent method not taking a callback interface so that an expensive object could be put into cache only if it is absent from the cache. While waiting for such a thing to exist I defined my own cache interface with a method called getOrPut, which does the same thing through the use of a callback:code:
So I began to think of an alternative solution. What if I were to call concurrentHashMap.putIfAbsent(Key, ValueWrapper), where ValueWrapper is an object that contains the object to be cached, and an ICreateValue interface which creates the object. So for instance the flow would go like this: Client calls cache.getOrPut with a key and an ICreateValue callback to create the expensive object (most likely from db). ConcurrentMap stores the key and ValueWrapper with putIfAbsent. After ValueWrapper is retrieved from cache, call getValue() and return that to the client. ValueWrapper would look like this: code:
I'd really appreciate thoughts about this, especially if I am missing something.
|
# ¿ Jul 7, 2010 13:37 |
|
I've been spoiled by web frameworks for too long and now I need to write a gritty ServletFilter myself and I need a little help on how to do it. The purpose of the filter is to run last in the filter chain, take all the html that is meant to be returned as the response, and strip out all the content except for the elements that match a given id, and return the fragmented response. Parsing aside, how do I go about getting the html body from the response, and resend a modified version of it? How do I ensure that the response won't get partially flushed while I'm working on it?
|
# ¿ Dec 13, 2011 15:46 |
|
Thanks thats a great high level explanation, I'll get looking into that. Appreciate it
|
# ¿ Dec 13, 2011 21:00 |
|
rhag posted:The most common answer I've seen on the forums from the devs is to use a Set. I can see their reasoning, and it's a sound one. However, using a Set poses a different set of problems (namely, one cannot just add children to the parent without an ID set. That is...before the children are saved). This doesn't have to be a limitation. My solution to the equals/hashcode problem with hibernate is two have two primary keys, an autoincremented pk, and a String UID which is just mapped as a unique field. However my hashcode/equals only bothers looking at the uid, which gets created on object creation through UUID.randomUID.toString. So this way you can put any object in a set before persisting it, and the equals will work after you persist it because hibernate will set the UUID it received from the table. Make sense?
|
# ¿ Dec 13, 2011 21:37 |
|
I was wondering if anyone had a clever solution to this problem. I have a method that takes a String of html, and a Set<String> of ids pertaining to elements in the html. The purpose is to take the original html, find all of the tags that have one of the matched ids, and return a new string fragment of html. So for example: Given this html and a set of ids {"1", "4"} code:
code:
The only thing I can think of is a sax parser solution, where I maintain a map of String id > String htmlFragment. This seems pretty tough to implement correctly and efficiently. I wonder if a regular expression might work, although that seems pretty ugly. I'd welcome any ideas. And yes the html is valid xml. FateFree fucked around with this message at 20:09 on Dec 15, 2011 |
# ¿ Dec 15, 2011 20:07 |
|
I'm trying to create a zip file on the fly containing a bunch of csv files to return from a servlet and its confusing as hell with writers and readers and streams. A little guidance would be great. Here are chunks of code I have that somehow need to work together:code:
|
# ¿ Jun 3, 2013 20:48 |
|
rhag posted:I don't really understand why one would need to use a library but anyway. From the looks of it, CSVWriter accepts a writer as an argument on the constructor. You can pass it a StringWriter as the writer to write to, and then just call stringWriter.toString() to get the data it wrote. The buffer will become something like Thanks for the info, I actually figured out a way to do it without the string writer, and just use the output stream for the zip directly: code:
|
# ¿ Jun 4, 2013 18:07 |
|
I need to collect metrics in memory and occasionally flush the list contents to the database. What's the best data structure to use that will be threadsafe while metrics are being collected, but that I can also switch out to dump the ones in memory to the database in a timer thread? Should I just use Collections.synchronizedList, and then every now and then swap references from a live list to a secondary list so metrics can be collected while the dumping is going on? Is there any simple pattern for doing this?
|
# ¿ Aug 6, 2013 22:38 |
|
Hard NOP Life posted:I think that a ConcurrentLinkedQueue will do just what you want without having to use a Collections.synchronizedList. However I don't think your approach to switching out the references is the correct way to do it. Hmm thanks lets see... the javadoc makes it sound like it may get new elements in which case I'd have the same concern about the job never ending. However in your first suggestion I like the fact that removeAll isn't called until the end of the dump job - if I do the dump in one transaction and it fails, its nice that those elements won't be lost and the next job can get them all. To do the copy should I just pass the original list into a ConcurrentLinkedQueue(collection) constructor? If so this should work out pretty well, I appreciate it. edit - well this turned out pretty easy: code:
FateFree fucked around with this message at 00:46 on Aug 7, 2013 |
# ¿ Aug 6, 2013 23:45 |
|
Kilson posted:Can't you just use .drainTo() on the queue? Just drain the queue to a new list, and it's atomic. Well theres no drainTo function, but I'd rather not drain the queue because if the save fails I'll lose all the metrics in memory. With removeAll I'll empty them only if they successfully save to the db, otherwise they'll stick around for the next attempt.
|
# ¿ Aug 7, 2013 03:24 |
|
Kilson posted:Of course, in cases where you *know* that you won't actually throw the checked exception (and there are many such cases), then do what you want. It just looked like you were advocating the Spring approach of wrapping *all* exceptions as runtime, which I still say loving sucks. What's wrong with having only runtime exceptions? Thats how new languages like c# handle exceptions. Checked exceptions have caused more pain and harm and general misuse than any other feature. All it takes is a @throws declaration in the comments and anyone whose calling your method can choose to handle a runtime exception or ignore it.
|
# ¿ Nov 14, 2013 17:09 |
|
I'm working on a large application that's locked into a terrible database that cannot perform. All of our queries are hitting an 'Index' table, which is a simple table with three columns: customerId, indexName, indexValue. It might contain things like: 1, FirstName, John - and a search by name query would be something like Select customerId from Index where indexName=FirstName and indexValue=John. The customerId is later joined to another set of tables. Changing the database is not an option - but we are able to move the indexing off of the database onto something else. I'm looking for the best suggestion. Right now we are considering Lucene, because some of the indexed fields require fuzzy searching. However there is very little document searches, we really only have name/value pairs that link back to a customer. Is there anything more practical than Lucene for this kind of indexing? We essentially just need a quicker way to maintain an index since our database can't do it.
|
# ¿ Dec 18, 2013 16:53 |
|
rhag posted:What do you mean by cannot perform? Has that index table indexes created for the indexName and indexValue fields? Such as CREATE INDEX IDX1 on Index (indexName,indexValue) ? Is customerId a primary key on the other tables? Or has an index made for it? We are running a Teradata database. It is not meant to be an operational database, it will never be an operational database, and trying to make it perform like an operational database will never work. This was big corporation political fuckery that cannot be unfucked so it needs to be taken as a constant. (It is so bad that we are now storing our ENTIRE data model as a json object in one table, thus we have no indexes because we essentially have no data model). I can go on for days about how terrible this situation is but it will not help as this is the situation and no one will change it. So the problem is that we have a JSON blob as our entire database and no way to query it, thus the terrible index table was born. Even that has been tuned to poo poo by Teradata's experts and its not good enough. So whats are some alternative choices for an indexing application? We would really like something thats already built and open sourced - our best choice at the moment is Lucene because it can handle fuzzy searching on some of these index values like email and first name. Are there any other options that might work?
|
# ¿ Dec 19, 2013 19:09 |
|
Are you sure its not just the first line and the argument text isn't null? I'd bet thats the problem.
|
# ¿ Jan 10, 2014 00:05 |
|
Sab669 posted:Any recommendations on some absolute bare-bones First Programming Language books for Java? My roommate is going into his second semester of Comp Sci and he's really struggling. Decided to wait until the last week of vacation to buy a book and try to get back into it before the next semester starts I like the Head First java series for complete beginners. It has pictures.
|
# ¿ Jan 13, 2014 15:44 |
|
Can't you just have an AbstractOptions class that your base method uses, and have all your subclasses have inner classes that extend it? Subclass.Options extends AbstractOptions? Otherwise you'll just have a base class that has generics, BaseClass<? extends AbstractOptions>, and your subclasses SubClass<SubClass.Options>, and Subclass.Options extends AbstractOptions. This will let you do things like SubClass.Options options = subClass.getOptions(); baka kaba posted:I could just have the method accept everything and do an internal type check and save myself the headaches This will cause the headaches, not save them! FateFree fucked around with this message at 21:14 on Jan 22, 2014 |
# ¿ Jan 22, 2014 21:08 |
|
baka kaba posted:The trouble (I think) I've been running into is having a generic class like Subclass<Subclass.Options>, which seems to work fine for the definitions - but when it comes to using it that inner Subclass is considered a raw type, and there are obviously complaints. I'll have to take a look at exactly what is going wrong, but I'm just worried that it's a sign that I shouldn't even be trying to do this, at least not this way Generics in Java are notoriously horrible, don't take any warnings as a sign you are doing the wrong thing. In some cases, suppressing warnings are the only thing you can do. But in your case, I don't see how it wouldn't work..if its complaining about raw types that means you arent declaring SubClass<Subclass.Options> everywhere you use it. Declaring the inner classes static is essentially the same thing as storing them in a separate class file.
|
# ¿ Jan 22, 2014 21:45 |
|
Tusen Takk posted:Gaze that abortion of code and shame me Your code would have been ten times more readable if you just gave your labels and inputs actual names instead of label1, label2. Aren't you just forcing yourself to memorize what these things mean when you modify your code? Think about how much easier it would be if they were named commandTextField and commandLabel. And use imports for cryin out loud haha. You'll remove 30% of your code if you remove javax.swing everywhere. Edit: nevermind looks like you are using something to auto generate terrible code. FateFree fucked around with this message at 14:17 on Mar 15, 2014 |
# ¿ Mar 15, 2014 14:09 |
|
|
# ¿ Apr 27, 2024 13:19 |
|
Drunkenboxer posted:Here ya go, Do you have your MovieNode class available? I'm bored at work and I'm going to go through and clean up your code with some pointers.
|
# ¿ Apr 14, 2014 17:14 |