/*
 * Copyright 2012-2017 JetBrains s.r.o
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package jetbrains.jetpad.model.id;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;

public final class BaseIdGenerator {
  private static final int DEFAULT_NUM_IDS = 5;
  private static final String USAGE = "usage: num_ids file_to_save_in";

  public static void main(String[] args) {
    int numIds;
    if (args.length >= 1) {
      try {
        numIds = Integer.parseInt(args[0]);
      } catch (Exception e) {
        System.out.println(USAGE);
        return;
      }
    } else {
      numIds = DEFAULT_NUM_IDS;
    }

    OutputStream out;
    if (args.length >= 2) {
      try {
        out = new FileOutputStream(args[1]);
      } catch (Exception e) {
        System.out.println(USAGE);
        return;
      }
    } else {
      out = System.out;
    }

    try (PrintWriter writer = new PrintWriter(out)) {
      for (int i = 0; i < numIds; i++) {
        String idString = "\"" + new MyId().getId() + "\"";
        if (i == 0) {
          copyToClipboard(idString);
        }
        writer.println(idString);
      }
    }
  }

  private static void copyToClipboard(String idString) {
    StringSelection stringSelection = new StringSelection(idString);
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    clipboard.setContents(stringSelection, null);
  }

  private BaseIdGenerator() {
  }

  private static class MyId extends BaseId {}
}
@@ -742,16 +742,54 @@
 ection;%0A
+import java.io.FileNotFoundException;%0A
 import j
@@ -805,32 +805,32 @@
 leOutputStream;%0A
-
 import java.io.O
@@ -1179,32 +1179,44 @@
 %0A      %7D catch (
+NumberFormat
 Exception e) %7B%0A 
@@ -1201,33 +1201,39 @@
 FormatException 
-e
+ignored
 ) %7B%0A        Syst
@@ -1453,16 +1453,28 @@
  catch (
+FileNotFound
 Exceptio
@@ -1475,17 +1475,23 @@
 ception 
-e
+ignored
 ) %7B%0A    
@@ -2206,7 +2206,11 @@
 Id %7B
+%0A  
 %7D%0A%7D
+%0A

fix "Overly broad 'catch' block", "Unused 'catch' parameter" warnings (BaseIdGenerator)