@@ -56,71 +56,73 @@ public ASCIIGraph(final String fileName) throws Exception {
5656 public ASCIIGraph (final String fileName , final boolean inLinks ) throws Exception {
5757 int i ,j ;
5858 this .graphName =fileName ;
59- BufferedReader reader =new BufferedReader (new FileReader (fileName +".net" ));
60- N =Integer .parseInt (reader .readLine ());
61- if (N <=0 ) {
62- reader .close ();
63- throw new ASCIIGraphFormatException ("Network dimension " +N +" less than or euqal to 0!" );
64- }
65- this .indegrees =null ;
66- this .inLinks =inLinks ;
67- long edges =0 ;
68- this .nodes =new Node [N ];
69- for (i =0 ;i <N ;i ++) {
70- String s =reader .readLine ();
71- if (s ==null ) break ;
72- String [] tokens =s .split (" " );
73- final int degree =tokens .length -1 ;
74- int id =Integer .parseInt (tokens [0 ]);
75- if (id <0 || id >=N ) {
76- reader .close ();
77- throw new ASCIIGraphFormatException ("Node out of bounds: node " +id +
78- " is greater than or equal to the network dimension " +N +"!" );
79- }
80- if (this .nodes [id ]!=null ) {
81- reader .close ();
82- throw new ASCIIGraphFormatException ("Wrong network format: multiple " +id +"-node lines!" );
83- }
84- Node node =new Node (id ,degree );
85- this .nodes [id ]=node ;
86- for (j =1 ;j <tokens .length ;j ++) {
87- edges ++;
88- id =Integer .parseInt (tokens [j ]);
89- if (id <0 || id >=N ) {
90- reader .close ();
91- throw new ASCIIGraphFormatException ("Node out of bounds: node " +id +
92- " is greater than or equal to the network dimension " +N +"!" );
93- }
94- node .addOutLink (id );
95- }
96- }
97- this .edges = edges ;
98- if (!inLinks ) {
99- for (i =0 ;i <N ;i ++) {
100- if (this .nodes [i ]==null ) this .nodes [i ]=new Node (i ,0 );
101- else this .nodes [i ].sort ();
102- }
103- } else {
104- for (i =0 ;i <N ;i ++) {
105- if (this .nodes [i ]==null ) {
106- this .nodes [i ]=new Node (i ,0 );
107- } else {
108- final int [] out =nodes [i ].getOutLinks ();
109- for (j =0 ;j <out .length ;j ++) {
110- if (nodes [out [j ]]==null ) nodes [out [j ]]=new Node (i ,0 );
111- nodes [out [j ]].incInDegree ();
112- }
113- }
114- }
115- for (i =0 ;i <N ;i ++) {
116- final int [] out =nodes [i ].getOutLinks ();
117- for (j =0 ;j <out .length ;j ++)
118- nodes [out [j ]].addInLink (i );
119- }
120- for (i =0 ;i <N ;i ++)
121- this .nodes [i ].sort ();
122- }
123-
59+ try (FileReader fr = new FileReader (fileName +".net" )) {
60+ try (BufferedReader reader =new BufferedReader (fr )) {
61+ N =Integer .parseInt (reader .readLine ());
62+ if (N <=0 ) {
63+ reader .close ();
64+ throw new ASCIIGraphFormatException ("Network dimension " +N +" less than or euqal to 0!" );
65+ }
66+ this .indegrees =null ;
67+ this .inLinks =inLinks ;
68+ long edges =0 ;
69+ this .nodes =new Node [N ];
70+ for (i =0 ;i <N ;i ++) {
71+ String s =reader .readLine ();
72+ if (s ==null ) break ;
73+ String [] tokens =s .split (" " );
74+ final int degree =tokens .length -1 ;
75+ int id =Integer .parseInt (tokens [0 ]);
76+ if (id <0 || id >=N ) {
77+ reader .close ();
78+ throw new ASCIIGraphFormatException ("Node out of bounds: node " +id +
79+ " is greater than or equal to the network dimension " +N +"!" );
80+ }
81+ if (this .nodes [id ]!=null ) {
82+ reader .close ();
83+ throw new ASCIIGraphFormatException ("Wrong network format: multiple " +id +"-node lines!" );
84+ }
85+ Node node =new Node (id ,degree );
86+ this .nodes [id ]=node ;
87+ for (j =1 ;j <tokens .length ;j ++) {
88+ edges ++;
89+ id =Integer .parseInt (tokens [j ]);
90+ if (id <0 || id >=N ) {
91+ reader .close ();
92+ throw new ASCIIGraphFormatException ("Node out of bounds: node " +id +
93+ " is greater than or equal to the network dimension " +N +"!" );
94+ }
95+ node .addOutLink (id );
96+ }
97+ }
98+ this .edges = edges ;
99+ if (!inLinks ) {
100+ for (i =0 ;i <N ;i ++) {
101+ if (this .nodes [i ]==null ) this .nodes [i ]=new Node (i ,0 );
102+ else this .nodes [i ].sort ();
103+ }
104+ } else {
105+ for (i =0 ;i <N ;i ++) {
106+ if (this .nodes [i ]==null ) {
107+ this .nodes [i ]=new Node (i ,0 );
108+ } else {
109+ final int [] out =nodes [i ].getOutLinks ();
110+ for (j =0 ;j <out .length ;j ++) {
111+ if (nodes [out [j ]]==null ) nodes [out [j ]]=new Node (i ,0 );
112+ nodes [out [j ]].incInDegree ();
113+ }
114+ }
115+ }
116+ for (i =0 ;i <N ;i ++) {
117+ final int [] out =nodes [i ].getOutLinks ();
118+ for (j =0 ;j <out .length ;j ++)
119+ nodes [out [j ]].addInLink (i );
120+ }
121+ for (i =0 ;i <N ;i ++)
122+ this .nodes [i ].sort ();
123+ }
124+ }
125+ }
124126 }
125127
126128 @ Override
0 commit comments