1- /*
2- * Esta classe representa um objeto para uma entidade
3- * que será armazenado em uma árvore B+.
4- *
5- * Neste caso em particular, este objeto é representado
6- * por dois números inteiros para que possa conter
7- * relacionamentos entre dois IDs de entidades quaisquer.
8- *
9- * Implementado pelo Prof. Marcos Kutova
10- * v1.0 - 2021
11- */
121package tp01 .src .storage .indexes ;
132
14- import java .io .ByteArrayInputStream ;
15- import java .io .ByteArrayOutputStream ;
16- import java .io .DataInputStream ;
17- import java .io .DataOutputStream ;
18- import java .io .IOException ;
3+ import java .io .*;
194
205import tp01 .src .storage .records .RegisterTreeB ;
216
22- /**
23- * Classe que representa um par de IDs para ser armazenado em uma estrutura de
24- * árvore B+.
25- * Usado para representar relacionamentos entre registros de duas entidades.
26- */
277public class PairIDFK implements RegisterTreeB <PairIDFK > {
288
29- private int id ; /** ID da entidade principal (chave primária). */
30- private int fk ; /** ID da entidade relacionada (chave estrangeira). */
31- private short TAMANHO = 8 ; /** Tamanho fixo da estrutura serializada (em bytes). */
9+ private int fk ; // Chave estrangeira — identificador da entidade relacionada
10+ private int id ; // Chave primária — identificador da entidade principal
11+ private short TAMANHO = 8 ;
3212
33- /**
34- * Construtor padrão. Inicializa ambos os campos com -1.
35- */
3613 public PairIDFK () {
3714 this (-1 , -1 );
3815 }
3916
40- /**
41- * Construtor que inicializa apenas a chave estrangeira (FK), com ID -1.
42- *
43- * @param n1 Valor da chave estrangeira.
44- */
45- public PairIDFK (int n1 ) {
46- this (n1 , -1 );
17+ public PairIDFK (int fk ) {
18+ this (fk , -1 );
4719 }
4820
49- /**
50- * Construtor que inicializa os dois campos: ID e FK.
51- *
52- * @param n1 ID da entidade principal.
53- * @param n2 ID da entidade relacionada (FK).
54- */
55- public PairIDFK (int n1 , int n2 ) {
56- try {
57- this .id = n1 ; // ID da tabela principal (N)
58- this .fk = n2 ; // ID da tabela secundária (1)
59- } catch (Exception ec ) {
60- ec .printStackTrace ();
61- }
62- }
63-
64- /**
65- * Retorna o ID da entidade principal.
66- *
67- * @return ID.
68- */
69- public int getId () {
70- return id ;
21+ public PairIDFK (int fk , int id ) {
22+ this .fk = fk ;
23+ this .id = id ;
7124 }
7225
73- /**
74- * Retorna a chave estrangeira (FK).
75- *
76- * @return FK.
77- */
7826 public int getFk () {
7927 return fk ;
8028 }
8129
82- /**
83- * Cria e retorna uma cópia deste objeto.
84- *
85- * @return Clone de PairIDFK.
86- */
30+ public int getId () {
31+ return id ;
32+ }
33+
8734 @ Override
8835 public PairIDFK clone () {
89- return new PairIDFK (this .id , this .fk );
36+ return new PairIDFK (this .fk , this .id );
9037 }
9138
92- /**
93- * Retorna o tamanho da estrutura em bytes.
94- *
95- * @return Tamanho em bytes (8).
96- */
9739 public short size () {
9840 return this .TAMANHO ;
9941 }
10042
101- /**
102- * Compara dois objetos {@code PairIDFK} com base em seus IDs e FKs.
103- * A comparação dá prioridade ao ID, e em caso de empate, à FK.
104- *
105- * @param a Outro objeto PairIDFK.
106- * @return Valor negativo, zero ou positivo conforme a ordenação.
107- */
43+ @ Override
10844 public int compareTo (PairIDFK a ) {
109- if (this .fk == -1 ) {
110- return this .id - a .id ;
45+ System .out .println ("FK (chave estrangeira): " + this .fk );
46+ System .out .println ("ID (chave primária): " + this .id );
47+
48+ if (this .id == -1 ) {
49+ return this .fk - a .fk ;
11150 }
112- if (this .id != a .id ) {
113- return this .id - a .id ;
51+
52+ if (this .fk != a .fk ) {
53+ return this .fk - a .fk ;
11454 }
115- return this .fk == -1 ? 0 : this .fk - a .fk ;
55+
56+ return this .id == -1 ? 0 : this .id - a .id ;
11657 }
11758
118- /**
119- * Retorna uma representação em string do par ID;FK.
120- *
121- * @return String formatada com os dois valores.
122- */
12359 public String toString () {
124- return String .format ("%3d" , this .id ) + ";" + String .format ("%-3d" , this .fk );
60+ return String .format ("%3d" , this .fk ) + ";" + String .format ("%-3d" , this .id );
12561 }
12662
127- /**
128- * Serializa o objeto em um array de bytes.
129- *
130- * @return Array de bytes com os dados serializados.
131- * @throws IOException Se ocorrer erro na escrita.
132- */
13363 public byte [] toByteArray () throws IOException {
13464 ByteArrayOutputStream baos = new ByteArrayOutputStream ();
13565 DataOutputStream dos = new DataOutputStream (baos );
136- dos .writeInt (this .id );
13766 dos .writeInt (this .fk );
67+ dos .writeInt (this .id );
13868 return baos .toByteArray ();
13969 }
14070
141- /**
142- * Desserializa os dados de um array de bytes para preencher o objeto.
143- *
144- * @param ba Array de bytes com os dados serializados.
145- * @throws IOException Se ocorrer erro na leitura.
146- */
14771 public void fromByteArray (byte [] ba ) throws IOException {
14872 ByteArrayInputStream bais = new ByteArrayInputStream (ba );
14973 DataInputStream dis = new DataInputStream (bais );
150- this .id = dis .readInt ();
15174 this .fk = dis .readInt ();
75+ this .id = dis .readInt ();
15276 }
153-
154- }
77+ }
0 commit comments