For questions regarding utility class methods.
Questions tagged [utility-method]
69 questions
5
votes
2 answers
Apache Commons toString utility that only includes fields that have value
Is there a toString utility from Apache Commons that will only include in the resulting toString value those fields that are NOT null?
For example:
public class Person {
String name;
String height;
String age;
}
And create an instance where it has…

Carlos Jaime C. De Leon
- 2,476
- 2
- 37
- 53
5
votes
3 answers
Javascript utility function to convert a class - "constructor function" into a Singleton
I have some reading about the two ways of creating a singleton in javascript - simple object literal way and another with closure technique if we want to use private variables.
I am looking to create a utility function, for example…

Anmol Saraf
- 15,075
- 10
- 50
- 60
4
votes
3 answers
Is there a utility method for creating a list with specified size and contents?
public static List repeat(T contents, int length) {
List list = new ArrayList();
for (int i = 0; i < length; i++) {
list.add(contents);
}
return list;
}
This is a utility method in our proprietary commons…

piepera
- 2,033
- 1
- 20
- 21
4
votes
3 answers
guava-libraries: List with n instances
The Java Collections class has the following method:
static List nCopies(int n, T o)
I need a similar method, but slightly more generic, which provides n instances of a given class. Something like:
static List nInstances(int n,…

Otavio Macedo
- 1,542
- 1
- 13
- 34
3
votes
3 answers
Is there an existing Java utility method that can join a list of Strings on "," and "and"?
I'm looking for something to augment the function of the apache commons join() function, basically that will do what makePrettyList() does
public String makePrettyList(List items) {
String list =…

slk
- 285
- 1
- 2
- 8
3
votes
4 answers
Java Anonymous Class as Utility Functions ? To design Arguments that are actually used, or one Argument (the bigger obj)
The Situation is that I have to use Function pointers for so many functions in Java (so I did it this way) and saved each anonymous class to a static variable of the Interface, so that I could use them directly.
/** The Constant HARDLIM. */
…

Ismail Marmoush
- 13,140
- 25
- 80
- 114
3
votes
1 answer
Deciding between utility class or inherit from a base activity
This is more of a best practice / performance related question specific to Android.(I understand this could be a generic discussion but I wanted it to be narrowed down taking into account the Android environment).
Lets say I have a method (which…

RmK
- 1,408
- 15
- 28
3
votes
3 answers
Is there a utility method to separate a list by given string?
Is there something like the following in Apache Common Lang or Spring Utils or do you write your own Util method for this?
List list = new ArrayList();
list.add("moo");
list.add("foo");
list.add("bar");
String enumeratedList =…

Christopher Klewes
- 11,181
- 18
- 74
- 102
2
votes
1 answer
Criteria for putting functionality in an API or letting developers make it themselves
I'm defining an abstract base class as a part of an API that will be used by other developers, kind of like defining the Android API and letting developers use it to make phone apps. When should the creator of the API (that's me) provide certain…

Chris Morris
- 4,335
- 4
- 24
- 28
2
votes
2 answers
Need something like getTotal(ClassName, ListOfObjectsOfClass, numericFieldOfClass)
I have class say for example public class Item {
int price;
String name;
// getters and setters
}
I have such 1000 or more objects (just example).
For each item there is a different price. And All this item objects are in List- my…

java_enthu
- 2,279
- 7
- 44
- 74
2
votes
1 answer
Where to put a small utility function that I would like to use across multiple packages/projects that I develop?
Right now I have one function that would be useful in a number of distinct packages that I work on. The function is only a handful of lines. But I would like to be able to use this code in a number of packages/projects that I work on and are…

Jagerber48
- 488
- 4
- 13
2
votes
1 answer
How to write Dart idiomatic utility functions or classes?
I am pondering over a few different ways of writing utility classes/functions. By utility I mean a part of code being reused in many places in the project. For example a set of formatting functions for the date & time handling.
I've got Java…

PrzemekTom
- 1,328
- 1
- 13
- 34
2
votes
2 answers
generate text tree for a non-existant folder structure
I want to generate a beautiful text rendering of a file system / folder structure that does not exist. Imagine I want to propose a file system layout, I would like to express this in an easy way I can quickly type down. For instance, given an input…

patzm
- 973
- 11
- 23
2
votes
1 answer
Where to put utility methods in Ruby superclass
I'm writing a Ruby object that will be used as the superclass of arbitrary classes that inherit from it. The class has a couple well-defined methods, along with many small utility methods, to factor out some work from the main methods. I want those…

jrdioko
- 32,230
- 28
- 81
- 120
2
votes
1 answer
Where should I put common utility methods in an Asp.Net Core MVC-project?
I currently have all my common utility function methods in a base controller, which all of my controllers inherits from. These are methods for functionality like uploading files, resizing pictures, deleting files, sending e-mails, generating random…

Stian
- 1,522
- 2
- 22
- 52