How to replace string group with a string in eclipse - java

I have a set of constant like this:
private static final transient String NOME_FILE_RC = "blabla1.xml";
private static final transient String NOME_FILE_NS = "blabla2.xml";
private static final transient String NOME_FILE_NE = "blabla3.xml";
private static final transient String NOME_FILE_DT = "blabla4.xml";
private static final transient String NOME_FILE_MC = "blabla5.xml";
private static final transient String NOME_FILE_NR = "blabla6.xml";
I want to replace FILE with DOCUMENT in eclipse
I set find with \w{4}_(FILE)_\w{2}
I know that $1 catches FILE string but I don't know how to replace it with DOCUMENT.

Use (\w{4}_)FILE(_\w{2}) instead and replace with $1DOCUMENT$2.

capture the rest of the string
(.*?)FILE(.*)
and replace with
$1DOCUMENT$2

Related

Would setting a field in a class as a new instance of the same class cause memory overflow? InetValidator class (apache.commons)

I am using InetValidator class from apache.commons in my project to validate IP addresses. While going through the source code,
public class InetAddressValidator implements Serializable {
private static final int IPV4_MAX_OCTET_VALUE = 255;
private static final int MAX_UNSIGNED_SHORT = 65535;
private static final int BASE_16 = 16;
private static final long serialVersionUID = -919201640201914789L;
private static final String IPV4_REGEX = "^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$";
private static final int IPV6_MAX_HEX_GROUPS = 8;
private static final int IPV6_MAX_HEX_DIGITS_PER_GROUP = 4;
private static final InetAddressValidator VALIDATOR = new InetAddressValidator();
private final RegexValidator ipv4Validator = new RegexValidator("^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$");
public InetAddressValidator() {
}
public static InetAddressValidator getInstance() {
return VALIDATOR;
}
I observed there is a VALIDATOR field which is calling the constructor again.
What I'm confused with is if I create a new instance of this class as,
InetAddressValidator addressValidator = new InetAddressValidator();
won't it will recursively keep creating new instances of the same class (as the field VALIDATOR creates a new instance everytime) and eventually I'm gonna run out of heap space?
I haven't tested this yet, but wondering if this is something I have to keep in mind while testing.
private static final InetAddressValidator VALIDATOR = new InetAddressValidator();
it's a static field, so it will be instantiated just once at class load time, not per-instance in the constructor.
Recursive call
InetAddressValidator addressValidator = new InetAddressValidator(); Recursive call

Multiple constants in same class

I want to know what is the best way to use final constants in the same class? since i have to use a lot for the same class and looks very meshy, thanks.
How i can make it look more readable, since if I need to store more variables there will be many........... thanks
public class Default {
private static final String CLASS_FILE_EXTENSION = ".class";
private static final String JAVA_PACKAGE = "java.";
private static final String NO_PREFIX = "";
private static final String SUFFIX = "sfx";
private static final String TEMP_SUFFIX = "tmp";
private static final String NO_PARAMETERS = "()";
private static final String STRING_DESCRIPTOR = "Ljava/lang/String;";
private static final String RETURNS_VOID = "V";
private static final String JAR = "jar";
private static final int ONLY = 0;
private static final int BUFFER_SIZE = 1024;
private static final int PACKAGE_LOOKUP = 0x8;
private static final int BASE_VERSION = 44;
private static final Class<?> NOT_FOUND = null;
private static final URL NOT_SEALED = null;
private final TypeDescription instrumentedType;
private final TypeAttributeAppender typeAttributeAppender;
private final AsmVisitorWrapper asmVisitorWrapper;
private final ClassFileVersion classFileVersion;
private final AuxiliaryType.NamingStrategy auxiliaryTypeNamingStrategy;
private final AnnotationValueFilter.Factory annotationValueFilterFactory;
private final AnnotationRetention annotationRetention;
private final Implementation.Context.Factory implementationContextFactory;
private final MethodGraph.Compiler methodGraphCompiler;
private final TypeValidation typeValidation;
private final ClassWriterStrategy classWriterStrategy;
private final LatentMatcher<? super MethodDescription> ignoredMethods;
private final List<DynamicType> auxiliaryTypes;
private final ClassFileLocator classFileLocator;
public Default(..... etc)
There is no objective answer possible for this. A few points to consider:
Having a lot of constants in one class might mean that the class is doing a lot of work. See if you can split the class into multiple classes and declare the constants as per the usage within those classes.
Since all these are private fields, see if they are really being used more than once in the same class. If not you can directly use the string rather than having a constant for that. The downside is, in the future, if you want to use it again in the same class at a different place, you have to remember to make it a constant (which is very easy to forget).
(I don't prefer this) I have seen cases where people will have an interface just for holding constants and static import them into the class to use it. That interface is like a big dumping ground for the constants.

Android SQLite errors while inserting records

I have tried many things and searched both SO and google, but this is an odd error. The error happens when I click a button which creates(inserts) a new level in the database.
Logcat follows:
http://pokit.org/get/?564ecab747237335f72006e8e3d5d633.jpg
There is more down but it's the same error.
And here is the code that does the insert. I'm using SQLiteAssetHelper by the way.
public void createLevel(Level lev){
ContentValues values = new ContentValues();
if(lev.getGameMode()=="SinglePlayer")
{
LevelSinglePlayer level=(LevelSinglePlayer)lev;
values.put(GAME_MODE_COLUMN, level.getGameMode());
values.put(DIFFICULTY_COLUMN, level.getDifficulty());
values.put(SINGLE_PLAYER_MODE_COLUMN, level.getSinglePlayerMode());
values.putNull(TIME_CHALLENGE_MODE_COLUMN);
values.put(POINTS_COLUMN, level.getPoints());
values.put(LIVES_COLUMN, level.getLives());
values.putNull(TIME_COLUMN);
values.put(SUFFIX_COLUMN, level.getSuffix());
values.put(SUFFIX_WORDS_COUNT_COLUMN, level.getSuffixWordsCount());
db.insert(LEVELS_TABLE, null, values);
Log.w("INSERT_NEW_LEVEL", "SUCCESSFUL INSERT");
}
else if(lev.getGameMode()=="TimeChallenge")
{
LevelTimeChallenge level= (LevelTimeChallenge)lev;
values.put(GAME_MODE_COLUMN, level.getGameMode());
values.put(DIFFICULTY_COLUMN, level.getDifficulty());
values.putNull(SINGLE_PLAYER_MODE_COLUMN);
values.put(TIME_CHALLENGE_MODE_COLUMN, level.getTimeChallengeMode());
values.put(POINTS_COLUMN, level.getPoints());
values.put(LIVES_COLUMN, level.getLives());
values.put(TIME_COLUMN, level.getTime());
values.put(SUFFIX_COLUMN, level.getSuffix());
values.put(SUFFIX_WORDS_COUNT_COLUMN, level.getSuffixWordsCount());
db.insert(LEVELS_TABLE, null, values);
Log.w("INSERT_NEW_LEVEL", "SUCCESSFUL INSERT");
}
}
I have three tables, created by the following statements:
CREATE TABLE IF NOT EXISTS words(_id INTEGER PRIMARY KEY, word TEXT, tezina INTEGER)
CREATE TABLE IF NOT EXISTS levels(_id INTEGER PRIMARY KEY, modIgre TEXT NOT NULL, tezina INTEGER NOT NULL, modSingle TEXT, modTime TEXT, targetpoints INTEGER, lives INTEGER, nastavak TEXT, brojRijeciNastavak INTEGER, locked BOOLEAN, earnedpoints INTEGER, kaladonts INTEGER, req2stars INTEGER, req3stars INTEGER)
CREATE TABLE IF NOT EXISTS highscores(_id INTEGER PRIMARY KEY, player TEXT, points INTEGER)
I can post anything needed quickly.
EDIT: Java consts
//konstante(nazivi tabela)
private static final String GAME_MODE_COLUMN = "modIgre";//mod igre - single, time
private static final String DIFFICULTY_COLUMN = "tezina";//tezina rijeci na levelu
private static final String SINGLE_PLAYER_MODE_COLUMN = "modSingle";//mod single playera
private static final String TIME_CHALLENGE_MODE_COLUMN = "modTime";//mod time challenga
private static final String POINTS_COLUMN = "points";
private static final String LIVES_COLUMN = "lives";//zivoti
private static final String TIME_COLUMN = "";//koliko vrijeme?
private static final String SUFFIX_COLUMN = "nastavak";//koji nastavak?
private static final String SUFFIX_WORDS_COUNT_COLUMN = "brojRijeciNastavak";//koliko
private static final String ID_COLUMN = "_id";
private static final String TARGET_POINTS_COLUMN = "targetpoints";
private static final String LOCKED_COLUMN = "locked";
private static final String KALADONTS_COLUMN = "kaladonts";
private static final String TWO_STARS_COLUMN = "req2stars";
private static final String THREE_STARS_COLUMN = "req3stars";
private static final String EARNED_POINTS_COLUMN = "earnedpoints";
private static final String LEVELS_TABLE = "levels";
Your TIME_COLUMN is an empty string, causing the invalid SQL to be created:
private static final String TIME_COLUMN = "";//koliko vrijeme?
You can replace it with your actual column name. Didn't see a suitable column in your CREATE TABLE so it's possible that you should remove the put(TIME_COLUMN) calls altogether.
There is extra ',' in ur query or u r forget to put first field in ur query so correct query is :
INSERT into levels(modIgre,modTime,modSingle,brojRijeciNastavak,nastavak,tezina,lives,points)Values(?,?,?,?,?,?,?,?)
Your primary Key seems to be null and values too.
INSERT into levels(,modIgre,modTime,modSingle,brojRijeciNastavak,nastavak,tezina,lives,points)Values(?,?,?,?,?,?,?,?)
it is a problem because you begin your query by ,.

What are the possible ways to store this data?

Well, I need this data to be used in at least two classes:
String navHome = "home";
String navAbout = "about";
String navUpload = "upload";
String navUploadProc = "uploadProc";
String navStartUpload = "startUpload";
String navContact = "contact";
String navSearch = "search";
String navManual = "manual";
String navBackToGmis = "backToGmis";
I was thinking of just pasting that into other classes, but then it violates the principles of programming, since it's repeating the same thing. Text and XML files are not comfortable to store data, inheritance isn't an option, since Java doesn't support multiple inheritance... Any ideas?
Yeah, I know it will sound retarded to most of you, but I need tipps on this.
If these are constants, then you can declare them in one class (or interface) and use them in another:
A.java:
public class A {
public static final String navHome = "home";
public static final String navAbout = "about";
public static final String navUpload = "upload";
public static final String navUploadProc = "uploadProc";
public static final String navStartUpload = "startUpload";
public static final String navContact = "contact";
public static final String navSearch = "search";
public static final String navManual = "manual";
public static final String navBackToGmis = "backToGmis";
. . .
}
B.java:
import static A.*; // or list each String in a separate import
public class B {
. . . // code can use nav* as if they were declared in class B
}

Using #SuppressWarnings("unused") annotation on declaration section

I have a basic question relating to the #SuppressWarnings("unused") annotation. Basically, I would like to suppress unused warnings on a block of code in my declarations section of my class. I would prefer not to suppress unused warnings on the entire class.
Currently, I am doing this:
public class MyClass {
//Constants
private final String ACCEPT = "Yes";
#SuppressWarnings("unused")
private final String DENY = "No"; //Not currently used
private final String TENTATIVE = "Maybe";
#SuppressWarnings("unused")
private final String POSTPONE = "Later"; //Not currently used
//Variables
private int counter;
....
I find the above code to be a bit messy with the annotations thrown in randomly like that. What I would like to do is something like:
//Constants
#SuppressWarnings("unused")
{
private final String ACCEPT = "Yes";
private final String DENY = "No"; //Not currently used
private final String TENTATIVE = "Maybe";
private final String POSTPONE = "Later"; //Not currently used
}
//Variables
private int counter;
....
This is, of course, illegal in Java as a block cannot be simply made like this. Its not a huge deal or anything, but I am curious if I am missing an obvious and elegant solution.
If you reeeeealy want to keep all those unused constants, a quick and dirty trick would be;
public class MyClass {
//Variables
private int counter;
#SuppressWarnings("unused")
private static final class UnusedConstants{
private final String ACCEPT = "Yes";
private final String DENY = "No";
private final String TENTATIVE = "Maybe";
private final String POSTPONE = "Later";
}
}
And i would make them static too, but your call.
EDIT:
Or, what i think is much nicer:
public class MyClass {
private enum Answer {
ACCEPT ("Yes"),
DENY ("No"),
TENTATIVE("Maybe"),
POSTPONE("Later");
private final String answer;
private Answer(String answer) {
this.answer = answer;
}
public String toString() {
return answer;
}
}
}
This way you don't get the unused warning as long as you use at least one of the values.

Categories

Resources