• 1
  • 2
2020-09-26, 09:27
  #1
Medlem
Tips p hur man gr en sequence som i SQL fr att generera nummer, exempel fr en primary key.. Hur gr man motsvarande i Java?

Provade och kom fram till denna lsning:

Kod:
public class Test {
	
	static int PK = 1000; 
	
	Test () {
		this.PK = PK + 1;
	}
	
	public static void main(String [] args) {
			Test y1 = new Test();
			System.out.println(y1.PK);
			Test y2 = new Test();
			System.out.println(y2.PK);
	}	
}

Tycker att den verkar lite vl simpel. Finns det ett bttre, skrare stt med set/getters?
__________________
Senast redigerad av bosscs2 2020-09-26 kl. 09:38.
Citera
2020-09-26, 14:27
  #2
Medlem
a-mortals avatar
SQLSequence.java
Kod:
/**
Copyright 2020 a-mortal

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**/

public class SQLSequence
	implements java.util.Iterator<Integer>, Iterable<Integer>
{
	
	public static final boolean NOCYCLE = false, CYCLE = true;
	
	private int currentValue;
	private int initialValue;
	private int incrementValue;
	private int maximumValue;
	private boolean cycleType;
	private boolean hasNextValue = true;
	
	public SQLSequence (
		int initial_value,
		int increment_value,
		int maximum_value,
		boolean cycle_type
	) {
		if (initial_value > maximum_value)
			throw new IllegalArgumentException(
			"The maximum must be higher or equal to the inital value.");
		if (increment_value < 1)
			throw new IllegalArgumentException(
			"The increment value must be greater than zero.");
		
		initialValue = initial_value;
		incrementValue = increment_value;
		maximumValue = maximum_value;
		cycleType = cycle_type;
		
		currentValue = initialValue;
	}
	
	public final boolean hasNext() {
		return hasNextValue;
	}
	
	public final Integer next() {
		int x = currentValue;
		if (currentValue < maximumValue) currentValue++;
		else if (cycleType) currentValue = initialValue;
		else hasNextValue = false;
		return x;
	}
	
	public final void remove() { }
	
	public java.util.Iterator<Integer> iterator() {
		return this;
	}
	
}

Test.java
Kod:
public class Test {
	
	public static void main(String[] args) {
		Iterable<Integer> sequence = new SQLSequence(
			1000, 1, 1010,  SQLSequence.NOCYCLE
		);
		for (Integer i : sequence) {
			System.out.println(i);
		}
	}
	
}
__________________
Senast redigerad av a-mortal 2020-09-26 kl. 14:49.
Citera
2020-09-26, 14:56
  #3
Medlem
a-mortals avatar
Citat:
Ursprungligen postat av bosscs2
Kod:
public class Test {
	
	static int PK = 1000; 
	
	Test () {
		this.PK = PK + 1;
	}
	
	public static void main(String [] args) {
			Test y1 = new Test();
			System.out.println(y1.PK);
			Test y2 = new Test();
			System.out.println(y2.PK);
	}	
}

Den hr koden kommer inte gra det den ska fr att PK r statisk, dvs. Test.PK = y1.PK = y2.PK.
Citera
2020-09-26, 22:43
  #4
Medlem
Citat:
Ursprungligen postat av a-mortal
Den hr koden kommer inte gra det den ska fr att PK r statisk, dvs. Test.PK = y1.PK = y2.PK.

Du menar att fr varje nytt objekt s behller PK sitt vrde? Alla nya objekt fr samma vrde p PK? Hade ocks en sn fundering men provade och d funkade det trots "static"?
Citera
2020-09-26, 22:45
  #5
Medlem
Citat:
Ursprungligen postat av a-mortal
SQLSequence.java
Kod:
/**
Copyright 2020 a-mortal

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**/

public class SQLSequence
	implements java.util.Iterator<Integer>, Iterable<Integer>
{
	
	public static final boolean NOCYCLE = false, CYCLE = true;
	
	private int currentValue;
	private int initialValue;
	private int incrementValue;
	private int maximumValue;
	private boolean cycleType;
	private boolean hasNextValue = true;
	
	public SQLSequence (
		int initial_value,
		int increment_value,
		int maximum_value,
		boolean cycle_type
	) {
		if (initial_value > maximum_value)
			throw new IllegalArgumentException(
			"The maximum must be higher or equal to the inital value.");
		if (increment_value < 1)
			throw new IllegalArgumentException(
			"The increment value must be greater than zero.");
		
		initialValue = initial_value;
		incrementValue = increment_value;
		maximumValue = maximum_value;
		cycleType = cycle_type;
		
		currentValue = initialValue;
	}
	
	public final boolean hasNext() {
		return hasNextValue;
	}
	
	public final Integer next() {
		int x = currentValue;
		if (currentValue < maximumValue) currentValue++;
		else if (cycleType) currentValue = initialValue;
		else hasNextValue = false;
		return x;
	}
	
	public final void remove() { }
	
	public java.util.Iterator<Integer> iterator() {
		return this;
	}
	
}

Test.java
Kod:
public class Test {
	
	public static void main(String[] args) {
		Iterable<Integer> sequence = new SQLSequence(
			1000, 1, 1010,  SQLSequence.NOCYCLE
		);
		for (Integer i : sequence) {
			System.out.println(i);
		}
	}
	
}

Hahaha satan
__________________
Senast redigerad av bosscs2 2020-09-26 kl. 22:49.
Citera
2020-09-26, 22:45
  #6
Medlem
a-mortals avatar
Citat:
Ursprungligen postat av bosscs2
Du menar att fr varje nytt objekt s behller PK sitt vrde? Alla nya objekt fr samma vrde p PK? Hade ocks en sn fundering men provade och d funkade det trots "static"?

Testa med omflyttningen nedan s ska du se.

Kod:
public class Test {
	
	static int PK = 1000; 
	
	Test () {
		this.PK = PK + 1;
	}
	
	public static void main(String [] args) {
			Test y1 = new Test();
			Test y2 = new Test();
			System.out.println(y1.PK);
			System.out.println(y2.PK);
	}	
}
Citera
2020-09-26, 22:50
  #7
Medlem
askers avatar
Citat:
Ursprungligen postat av bosscs2
Du menar att fr varje nytt objekt s behller PK sitt vrde? Alla nya objekt fr samma vrde p PK? Hade ocks en sn fundering men provade och d funkade det trots "static"?
Din kod fungerar inte. Kolla vad y1.PK r efter att du skapat y2.PK. De refererar bda till samma statiska variabel.

Hll reda p inkrementvariabeln i en statisk variabel, och hll PrimaryKey i en instansvariabel
Kod:
public class Test {
	
	private static int PKIncrement = 1000;
	public int PK; 
	
	Test () {
		this.PK = PKIncrement++;
	}
	
	public static void main(String [] args) {
			Test y1 = new Test();
			Test y2 = new Test();
			System.out.println(y1.PK);
			System.out.println(y2.PK);
	}	
}
Citera
2020-09-26, 22:53
  #8
Medlem
Citat:
Ursprungligen postat av a-mortal
Testa med omflyttningen nedan s ska du se.

Kod:
public class Test {
	
	static int PK = 1000; 
	
	Test () {
		this.PK = PK + 1;
	}
	
	public static void main(String [] args) {
			Test y1 = new Test();
			Test y2 = new Test();
			System.out.println(y1.PK);
			System.out.println(y2.PK);
	}	
}

Har inget IDE tillgngligt, men vad funkar inte det dr?
Citera
2020-09-26, 22:58
  #9
Medlem
a-mortals avatar
Oups. Mrkte att jag skrev
Kod:
if (currentValue < maximumValue) currentValue++;
.
Jag skulle skrivit
Kod:
if (currentValue < maximumValue) currentValue += incrementValue;
Citera
2020-09-26, 23:01
  #10
Medlem
a-mortals avatar
Citat:
Ursprungligen postat av bosscs2
Har inget IDE tillgngligt, men vad funkar inte det dr?
Man fr
Kod:
1002
1002
Citera
2020-09-27, 20:29
  #11
Medlem
Citat:
Ursprungligen postat av a-mortal
Man fr
Kod:
1002
1002
Fr att en static variabel bara kan implementeras 1ggr? Gissar att final prefixet ger samma resultat.
Edit:
Sg nu att jag redan ftt svar.
Edit igen... :
Finns det en pong med att hlla PK som static?
__________________
Senast redigerad av bosscs2 2020-09-27 kl. 20:44.
Citera
2020-09-27, 22:13
  #12
Medlem
Neksnors avatar
Citat:
Ursprungligen postat av bosscs2
Fr att en static variabel bara kan implementeras 1ggr? Gissar att final prefixet ger samma resultat.
Edit:
Sg nu att jag redan ftt svar.
Edit igen... :
Finns det en pong med att hlla PK som static?
(Har bara lst trden lite hastigt...)

Ja, det r ju den som hller reda p nsta vrde att dela ut.
Citera
  • 1
  • 2

Skapa ett konto eller logga in för att kommentera

Du måste vara medlem för att kunna kommentera

Skapa ett konto

Det är enkelt att registrera ett nytt konto

Bli medlem

Logga in

Har du redan ett konto? Logga in här

Logga in