Why does object “one” forget part of its content when a method on object “two” is called?

StackOverflow https://stackoverflow.com/questions/21698695

  •  09-10-2022
  •  | 
  •  

Question

My program holds two objects, “one” and “two” of the same class. At the moment I call two.optimize(), “one” looses the content of its exclusions field:

Until here, everything is fine:
The New Press (1816-02-20 - 1922-06-05) [Morning issue (MTWTFS-) +[] -[1888-04-02, 1822-04-08, 1913-03-24, 1873-04-11, 1885-10-03, 1840-12-25, 1856-12-25, 1833-12-26, 1837-12-25, 1842-05-25, 1902-10-03, 1889-10-03, 1917-06-07, 1903-05-21, 1843-10-03, 1856-05-01, 1816-10-03, 1837-12-26, 1862-12-26, 1868-05-21, 1886-04-23, 1816-04-15, 1887-04-08, 1827-12-25, 1860-10-03, 1917-10-03, 1821-04-23, 1917-06-06, 1889-12-25, 1914-05-21, 1914-06-10, 1872-03-29, 1873-04-14, 1851-06-19, 1827-12-26, 1823-05-08, 1889-12-26, 1913-03-21, 1856-12-26, 1851-06-18, 1883-01-01, 1899-06-01, 1840-12-26, 1816-04-12, 1841-01-01, 1849-05-17, 1914-06-11, 1872-05-09, 1877-05-10, 1826-05-04, 1900-04-16, 1886-01-01, 1843-06-15, 1898-06-08, 1886-04-26, 1821-04-20, 1887-01-01, 1892-05-26, 1908-12-25, 1920-01-01, 1843-06-14, 1876-06-15, 1828-04-04, 1921-05-05, 1896-06-03, 1913-05-22, 1897-01-01, 1844-05-16, 1909-05-20, 1897-04-19, 1821-01-01, 1901-04-08, 1906-04-16, 1840-04-17, 1856-01-01, 1896-06-04, 1859-06-23, 1818-12-25, 1879-06-12, 1906-12-25, 1913-05-21, 1873-10-03, 1904-06-02, 1876-06-14, 1906-12-26, 1881-06-16, 1818-04-30, 1859-06-22, 1844-10-03, 1818-12-26, 1908-12-26, 1921-03-25, 1840-01-01, 1854-12-25, 1904-06-01, 1841-04-12, 1900-04-13, 1827-05-24, 1915-05-13, 1866-03-30, 1822-04-05, 1885-05-14, 1886-06-03, 1884-05-22, 1890-06-05, 1820-03-31, 1918-12-26, 1854-12-26, 1901-04-05, 1849-06-06, 1897-04-16, 1881-01-01, 1828-04-07, 1881-06-15, 1845-05-22, 1905-06-01, 1906-04-13, 1835-06-18, 1834-01-01, 1894-05-24, 1839-12-26, 1885-06-03, 1833-06-05, 1835-06-17, 1839-12-25, 1885-06-04, 1845-05-21, 1833-06-06, 1921-03-28, 1887-04-11, 1854-10-03, 1903-04-13, 1849-06-07, 1834-12-25, 1841-04-09, 1906-06-14, 1870-05-26, 1817-04-07, 1905-06-21, 1836-10-03, 1894-05-23, 1839-05-30, 1824-06-17, 1830-05-20, 1863-04-06, 1878-04-19, 1907-05-29, 1883-03-26, 1918-12-25, 1829-06-17, 1895-05-23, 1868-12-26, 1824-06-16, 1837-03-27, 1853-03-25, 1850-05-09, 1851-10-03, 1836-04-04, 1907-05-30, 1915-01-01, 1890-06-04, 1894-03-23, 1859-04-22, 1836-06-01, 1891-05-07, 1868-12-25, 1840-04-20, 1863-06-04, 1901-12-25, 1875-12-25, 1829-06-18, 1824-01-01, 1863-06-03, 1863-04-03, 1878-04-22, 1842-12-26, 1831-01-01, 1906-01-01, 1905-10-03, 1853-03-28, 1911-05-25, 1839-04-01, 1890-10-03, 1871-10-03, 1836-06-02, 1918-05-29, 1906-06-13, 1901-12-26, 1858-12-25, 1918-05-30, 1896-01-01, 1859-04-25, 1908-04-20, 1884-04-11, 1860-06-06, 1843-05-25, 1828-12-26, 1898-06-09, 1860-06-07, 1908-01-01, 1826-10-03, 1833-10-03, 1828-12-25, 1879-10-03, 1859-01-01, 1870-10-03, 1847-05-13, 1836-04-01, 1839-03-29, 1911-06-15, 1884-12-25, 1911-06-14, 1823-05-29, 1880-05-26, 1910-10-03, 1859-12-26, 1836-05-12, 1823-05-28, 1832-05-31, 1908-04-17, 1858-06-03, 1841-12-25, 1884-12-26, 1884-04-14, 1869-05-26, 1862-12-25, 1883-03-23, 1817-04-04, 1825-12-26, 1880-05-27, 1901-01-01, 1839-05-29, 1818-10-03, 1858-06-02, 1825-01-01, 1831-10-03, 1869-05-27, 1842-05-26, 1919-05-29, 1878-01-01, 1823-10-03, 1819-04-09, 1864-12-26, 1862-06-18, 1914-04-10, 1820-04-03, 1919-04-18, 1913-12-26, 1827-01-01, 1922-04-14, 1874-01-01, 1817-05-15, 1871-04-10, 1903-01-01, 1852-05-20, 1827-04-16, 1876-04-17, 1863-01-01, 1912-06-06, 1913-01-01, 1921-10-03, 1848-04-21, 1862-04-21, 1875-03-29, 1862-04-18, 1856-05-21, 1848-01-01, 1832-12-26, 1820-06-01, 1838-10-03, 1914-04-13, 1919-04-21, 1845-03-21, 1832-12-25, 1922-04-17, 1830-01-01, 1827-10-03, 1912-06-05, 1823-12-26, 1885-01-01, 1862-06-19, 1823-12-25, 1872-05-29, 1889-05-30, 1856-05-22, 1904-05-12, 1872-05-30, 1849-04-06, 1826-05-25, 1884-01-01, 1913-12-25, 1901-06-05, 1921-05-26, 1888-05-10, 1826-05-24, 1838-04-13, 1881-04-15, 1845-03-24, 1896-04-06, 1854-06-15, 1832-10-03, 1833-04-08, 1856-03-24, 1895-01-01, 1821-12-25, 1858-05-13, 1854-06-14, 1827-06-14, 1901-06-06, 1837-03-24, 1857-10-03, 1879-05-22, 1902-03-28, 1821-12-26, 1864-01-01, 1834-10-03, 1825-04-04, 1849-04-09, 1881-04-18, 1913-05-01, 1879-12-26, 1838-04-16, 1894-03-26, 1838-06-14, 1861-12-25, 1905-06-22, 1896-04-03, 1859-06-02, 1839-01-01, 1879-12-25, 1849-01-01, 1902-05-29, 1820-05-31, 1921-05-25, 1902-03-31, 1876-04-14, 1868-01-01, 1917-05-17, 1902-05-28, 1880-12-25, 1838-06-13, 1856-03-21, 1829-12-26, 1903-04-10, 1847-06-03, 1907-05-09, 1898-04-11, 1882-10-03, 1865-04-17, 1865-06-15, 1847-06-02, 1842-10-03, 1831-12-26, 1850-05-30, 1915-06-02, 1905-12-25, 1882-06-08, 1822-05-16, 1829-12-25, 1851-05-29, 1861-12-26, 1818-03-20, 1866-04-02, 1915-06-03, 1906-05-24, 1826-12-25, 1867-01-01, 1835-01-01, 1903-10-03, 1850-05-29, 1891-05-28, 1826-12-26, 1860-05-17, 1834-03-28, 1900-05-24, 1896-05-14, 1888-05-30, 1895-12-25, 1890-04-04, 1865-04-14, 1894-01-01, 1853-05-26, 1849-12-25, 1829-10-03, 1853-05-25, 1824-04-16, 1869-01-01, 1865-06-14, 1891-12-26, 1879-06-11, 1888-05-31, 1820-12-25, 1890-05-15, 1845-10-03, 1895-12-26, 1914-12-26, 1914-01-01, 1867-12-26, 1898-12-26, 1903-12-25, 1837-05-25, 1878-12-25, 1915-12-25, 1871-06-07, 1920-06-02, 1834-03-31, 1855-10-03, 1910-12-26, 1878-12-26, 1914-12-25, 1867-12-25, 1858-01-01, 1903-12-26, 1820-12-26, 1910-01-01, 1868-04-13, 1816-05-23, 1871-06-08, 1898-01-01, 1891-05-27, 1862-01-01, 1824-04-19, 1837-05-24, 1896-12-26, 1896-12-25, 1920-04-05, 1849-12-26, 1900-10-03, 1855-12-26, 1861-05-30, 1874-06-04, 1833-12-25, 1894-12-25, 1855-12-25, 1819-04-12, 1868-04-10, 1884-10-03, 1916-10-03, 1833-01-01, 1841-06-10, 1919-10-03, 1900-06-13, 1877-01-01, 1905-12-26, 1861-05-29, 1882-06-07, 1900-06-14, 1848-04-24, 1882-05-18, 1887-05-19, 1874-06-03, 1865-10-03, 1871-04-07, 1873-05-22, 1920-06-03, 1910-05-05, 1875-03-26, 1817-10-03, 1842-03-28, 1841-06-09, 1920-04-02, 1827-04-13, 1876-10-03, 1818-03-23, 1894-12-26, 1842-05-05, 1867-04-19, 1868-06-10, 1870-12-26, 1885-12-25, 1911-04-17, 1858-04-05, 1870-01-01, 1894-10-03, 1895-04-12, 1821-10-03, 1829-05-28, 1896-10-03, 1885-12-26, 1879-01-01, 1905-04-24, 1846-01-01, 1828-10-03, 1852-06-09, 1889-01-01, 1835-05-28, 1899-05-11, 1911-04-14, 1855-06-06, 1852-06-10, 1892-04-15, 1848-12-25, 1825-10-03, 1857-12-26, 1879-04-14, 1867-04-22, 1892-06-15, 1904-10-03, 1817-06-05, 1857-12-25, 1892-01-01, 1863-10-03, 1863-12-25, 1916-04-21, 1918-04-01, 1819-05-20, 1846-12-26, 1863-05-14, 1891-12-25, 1840-10-03, 1830-12-25, 1868-06-11, 1892-06-16, 1843-04-17, 1895-06-12, 1899-03-31, 1852-12-25, 1846-12-25, 1890-12-26, 1866-01-01, 1855-04-09, 1855-06-07, 1849-10-03, 1906-10-03, 1866-05-31, 1838-12-25, 1884-06-11, 1917-04-09, 1909-04-12, 1916-04-24, 1877-03-30, 1888-12-26, 1921-01-01, 1895-06-13, 1867-06-20, 1872-04-01, 1874-10-03, 1884-06-12, 1854-05-25, 1867-06-19, 1843-04-14, 1840-05-28, 1864-05-26, 1838-12-26, 1899-10-03, 1847-04-02, 1847-01-01, 1817-06-04, 1900-01-01, 1850-04-01, 1820-10-03, 1825-04-01, 1898-04-08, 1888-12-25, 1891-01-01, 1866-05-30, 1875-05-06, 1846-04-13, 1820-05-11, 1848-12-26, 1868-10-03, 1855-04-06, 1846-10-03, 1909-04-09, 1895-04-15, 1842-03-25, 1917-04-06, 1851-01-01, 1864-10-03, 1864-05-25, 1902-12-25, 1890-04-07, 1836-01-01, 1818-05-21, 1902-05-08, 1897-05-27, 1847-04-05, 1818-05-20, 1886-12-25, 1902-12-26, 1921-12-26, 1838-05-24, 1916-06-01, 1850-03-29, 1832-04-23, 1817-12-25, 1834-05-08, 1872-12-26, 1837-05-04, 1907-12-25, 1869-12-25, 1848-06-22, 1817-12-26, 1885-04-06, 1912-05-16, 1874-04-03, 1830-04-09, 1827-06-13, 1919-01-01, 1862-10-03, 1907-12-26, 1922-05-25, 1872-12-25, 1860-04-09, 1853-01-01, 1893-10-03, 1833-05-16, 1830-06-09, 1900-12-26, 1875-05-27, 1897-06-17, 1840-06-18, 1828-06-05, 1872-10-03, 1897-06-16, 1855-01-01, 1893-12-26, 1861-05-09, 1875-05-26, 1845-01-01, 1828-06-04, 1900-12-25, 1893-05-11, 1914-10-03, 1873-01-01, 1909-12-25, 1860-04-06, 1893-12-25, 1840-06-17, 1895-10-03, 1871-05-18, 1918-10-03, 1890-01-01, 1857-04-13, 1879-04-11, 1918-03-29, 1892-04-18, 1892-10-03, 1832-06-21, 1913-10-03, 1830-06-10, 1863-12-26, 1828-05-15, 1912-12-25, 1857-01-01, 1883-10-03, 1832-06-20, 1859-10-03, 1911-12-25, 1847-12-25, 1883-05-24, 1832-04-20, 1853-05-05, 1905-04-21, 1858-04-02, 1852-01-01, 1822-10-03, 1912-12-26, 1857-04-10, 1911-12-26, 1865-12-26, 1910-05-25, 1910-05-26, 1907-01-01, 1912-10-03, 1836-12-26, 1844-06-06, 1887-06-09, 1861-01-01, 1874-04-06, 1844-06-05, 1876-01-01, 1865-12-25, 1883-05-23, 1892-12-26, 1898-05-19, 1853-12-26, 1830-04-12, 1907-10-03, 1911-10-03, 1887-06-08, 1831-05-12, 1878-10-03, 1881-10-03, 1870-06-15, 1822-06-06, 1870-04-18, 1848-10-03, 1866-12-25, 1851-04-21, 1819-06-10, 1869-03-26, 1877-10-03, 1822-06-05, 1819-06-09, 1893-03-31, 1870-06-16, 1899-05-31, 1891-10-03, 1857-05-21, 1908-06-17, 1826-03-24, 1852-04-12, 1904-01-01, 1866-12-26, 1888-10-03, 1845-05-01, 1824-05-27, 1869-03-29, 1893-04-03, 1846-06-10, 1916-01-01, 1846-06-11, 1825-05-12, 1889-06-19, 1870-04-15, 1889-04-19, 1821-05-31, 1846-05-21, 1915-04-05, 1866-10-03, 1852-04-09, 1889-06-20, 1861-04-01, 1882-12-25, 1886-06-24, 1816-12-25, 1912-04-08, 1901-10-03, 1893-06-01, 1909-06-09, 1871-12-26, 1881-12-26, 1882-12-26, 1873-12-25, 1871-12-25, 1887-10-03, 1886-06-23, 1864-03-25, 1894-05-03, 1878-06-20, 1864-05-05, 1844-01-01, 1920-12-25, 1835-12-25, 1877-05-31, 1873-12-26, 1835-12-26, 1823-03-31, 1851-04-18, 1816-12-26, 1838-01-01, 1903-06-10, 1877-05-30, 1919-12-26, 1908-06-18, 1835-04-17, 1920-05-13, 1842-01-01, 1899-12-25, 1841-05-20, 1866-05-10, 1919-06-19, 1874-05-14, 1833-04-05, 1888-03-30, 1878-06-19, 1889-04-22, 1864-03-28, 1899-12-26, 1903-06-11, 1885-04-03, 1902-01-01, 1843-12-26, 1835-10-03, 1919-06-18, 1851-12-26, 1877-04-02, 1817-01-01, 1912-04-05, 1851-12-25, 1861-10-03, 1904-12-26, 1916-06-21, 1825-06-01, 1874-12-26, 1826-03-27, 1843-12-25, 1848-06-21, 1909-06-10, 1874-12-25, 1916-06-22, 1819-01-01, 1880-05-06, 1854-04-17, 1878-05-30, 1883-05-03, 1822-01-01, 1834-05-29, 1893-05-31, 1834-05-28, 1880-03-29, 1904-04-04, 1897-12-25, 1876-12-25, 1825-06-02, 1835-04-20, 1907-03-29, 1848-06-01, 1872-01-01, 1861-03-29, 1919-12-25, 1882-04-10, 1850-10-03, 1865-05-25, 1818-01-01, 1867-10-03, 1844-12-26, 1839-05-09, 1854-04-14, 1877-12-26, 1846-04-10, 1908-10-03, 1887-12-26, 1850-12-26, 1918-01-01, 1880-03-26, 1891-03-30, 1839-10-03, 1850-12-25, 1855-05-17, 1904-04-01, 1820-01-01, 1824-12-25, 1862-05-29, 1823-03-28, 1907-04-01, 1909-01-01, 1834-12-26, 1844-12-25, 1918-05-09, 1869-05-06, 1898-10-03, 1916-12-26, 1875-01-01, 1857-06-10, 1908-05-28, 1860-12-26, 1899-04-03, 1860-12-25, 1845-12-26, 1910-03-25, 1844-04-05, 1883-12-25, 1831-06-02, 1823-01-01, 1891-03-27, 1915-04-02, 1916-12-25, 1831-04-01, 1877-12-25, 1816-06-12, 1857-06-11, 1890-12-25, 1829-04-20, 1828-01-01, 1845-12-25, 1881-05-26, 1816-06-13, 1822-12-25, 1901-05-16, 1819-12-25, 1850-01-01, 1853-10-03, 1912-01-01, 1821-06-21, 1917-12-26, 1856-10-03, 1876-05-25, 1880-01-01, 1917-12-25, 1837-10-03, 1844-04-08, 1821-06-20, 1917-01-01, 1910-03-28, 1883-12-26, 1867-05-30, 1873-06-11, 1829-04-17, 1876-12-26, 1822-12-26, 1831-06-01, 1831-04-04, 1829-01-01, 1873-06-12, 1882-04-07]]

Suddanly, the content of “one” was modified:
The New Press (1816-02-20 - 1922-06-05) [Morning issue (MTWTFS-) +[] -[]]

I really don’t see why. It shouldn’t happen. The method operates on local variables only and there isn’t concurrency either… What do I miss? Any help is appreciated.

Here is the code:

Test.java: → test.Test.main() is the main class.

package test;

import java.util.HashSet;
import java.util.Set;

import org.goobi.production.model.bibliography.course.Issue;
import org.goobi.production.model.bibliography.course.Title;
import org.joda.time.DateTimeConstants;
import org.joda.time.LocalDate;

public class Test {

    public static void main(String[] args) throws Exception {
        String name = "The New Press";

        LocalDate beginn = new LocalDate(1816, DateTimeConstants.FEBRUARY, 20);
        LocalDate nameChange = new LocalDate(1932, DateTimeConstants.NOVEMBER, 1);

        Set<LocalDate> geschlossen = getExclusions(beginn, new LocalDate());

        Title one = new Title(name, beginn, new LocalDate(1922, 6, 5));
        Issue morningIssue = new Issue("Morning issue", true, true, true, true, true, true, false);
        morningIssue.setExclusions(geschlossen);
        one.addIssue(morningIssue);
        one.optimise();

        Title two = new Title(name, new LocalDate(1922, 6, 6), nameChange.minusDays(1));
        two.addIssue(morningIssue);
        Issue announcements = new Issue("Announcements", false, true, false, false, false, false, false);
        announcements.setExclusions(geschlossen);
        two.addIssue(announcements);

        System.out.println("Until here, everything is fine:");
        System.out.println(one.toString());

        two.optimise();

        System.out.println("\nSuddanly, the content of “one” was modified:");
        System.out.println(one.toString());
    }

    static Set<LocalDate> getExclusions(LocalDate from, LocalDate to) {
        Set<LocalDate> result = new HashSet<LocalDate>();
        for (int year = from.getYear(); year <= to.getYear(); year++) {
            LocalDate easter = gaussiannEasterAlgorithm(year);
            result.add(new LocalDate(year, DateTimeConstants.JANUARY, 1));
            result.add(easter.minusDays(2));
            result.add(easter.plusDays(1));
            result.add(easter.plusDays(39));
            result.add(easter.plusDays(59));
            result.add(easter.plusDays(60));
            result.add(new LocalDate(year, DateTimeConstants.OCTOBER, 3));
            result.add(new LocalDate(year, DateTimeConstants.DECEMBER, 25));
            result.add(new LocalDate(year, DateTimeConstants.DECEMBER, 26));
        }
        return result;
    }


    static LocalDate gaussiannEasterAlgorithm(int year) {
        int a = year % 19;
        int b = year / 100;
        int c = year % 100;
        int d = b / 4;
        int e = b % 4;
        int g = (8 * b + 13) / 25;
        int h = (19 * a + b - d - g + 15) % 30;
        int j = c / 4;
        int k = c % 4;
        int m = (a + 11 * h) / 319;
        int r = (2 * e + 2 * j - k - h + m + 32) % 7;
        int month = (h - m + r + 90) / 25;
        int day = (h - m + r + month + 19) % 32;
        return new LocalDate(year, month, day);
    }

}

Title.java:

package org.goobi.production.model.bibliography.course;

import java.util.HashSet;
import java.util.Set;

import org.joda.time.DateTimeConstants;
import org.joda.time.LocalDate;

/**
 * The class Title is a bean class that represents an interval of time in the
 * course of appearance of a newspaper within which it wasn’t suspended and
 * didn’t change its name either. A Title instance handles one or more Issue
 * objects.
 */
public class Title {
    protected String heading;
    protected LocalDate firstAppearance;
    protected LocalDate lastAppearance;
    protected Set<Issue> issues;

    public Title() {
    }

    public Title(String heading, LocalDate firstAppearance, LocalDate lastAppearance) {
        this.heading = heading;
        this.firstAppearance = firstAppearance;
        this.lastAppearance = lastAppearance;
        this.issues = new HashSet<Issue>();
    }

    public boolean addIssue(Issue e) {
        return issues.add(e);
    }

    /**
     * The method optimise() recalculates for each Issue the daysOfWeek of its
     * regular appearance within the interval of time of the Title.
     */
    public void optimise() {
        final int APPEARED = 1;
        final int NOT_APPEARED = 0;

        for (Issue issue : issues) {

            Set<LocalDate> remainingAdditions = new HashSet<LocalDate>();
            Set<LocalDate> remainingExclusions = new HashSet<LocalDate>();

            @SuppressWarnings("unchecked")
            HashSet<LocalDate>[][] subsets = new HashSet[DateTimeConstants.SUNDAY][APPEARED + 1];
            for (int dayOfWeek = DateTimeConstants.MONDAY; dayOfWeek <= DateTimeConstants.SUNDAY; dayOfWeek++) {
                subsets[dayOfWeek - 1][NOT_APPEARED] = new HashSet<LocalDate>();
                subsets[dayOfWeek - 1][APPEARED] = new HashSet<LocalDate>();
            }

            for (LocalDate day = firstAppearance; !day.isAfter(lastAppearance); day = day.plusDays(1))
                subsets[day.getDayOfWeek() - 1][issue.isMatch(day) ? APPEARED : NOT_APPEARED].add(day);

            for (int dayOfWeek = DateTimeConstants.MONDAY; dayOfWeek <= DateTimeConstants.SUNDAY; dayOfWeek++) {
                if (subsets[dayOfWeek - 1][APPEARED].size() > subsets[dayOfWeek - 1][NOT_APPEARED].size()) {
                    issue.addDayOfWeek(dayOfWeek);
                    remainingExclusions.addAll(subsets[dayOfWeek - 1][NOT_APPEARED]);
                } else {
                    issue.removeDayOfWeek(dayOfWeek);
                    remainingAdditions.addAll(subsets[dayOfWeek - 1][APPEARED]);
                }
            }

            issue.setAdditions(remainingAdditions);
            issue.setExclusions(remainingExclusions);
        }
    }

    @Override
    public String toString() {
        StringBuilder result = new StringBuilder();
        result.append(heading);
        result.append(" (");
        result.append(firstAppearance.toString());
        result.append(" - ");
        result.append(lastAppearance.toString());
        result.append(") [");
        boolean first = true;
        for (Issue issue : issues) {
            if (!first)
                result.append(", ");
            result.append(issue.toString());
            first = false;
        }
        result.append("]");
        return result.toString();
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((firstAppearance == null) ? 0 : firstAppearance.hashCode());
        result = prime * result + ((issues == null) ? 0 : issues.hashCode());
        result = prime * result + ((lastAppearance == null) ? 0 : lastAppearance.hashCode());
        result = prime * result + ((heading == null) ? 0 : heading.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (!(obj instanceof Title))
            return false;
        Title other = (Title) obj;
        if (firstAppearance == null) {
            if (other.firstAppearance != null)
                return false;
        } else if (!firstAppearance.equals(other.firstAppearance))
            return false;
        if (issues == null) {
            if (other.issues != null)
                return false;
        } else if (!issues.equals(other.issues))
            return false;
        if (lastAppearance == null) {
            if (other.lastAppearance != null)
                return false;
        } else if (!lastAppearance.equals(other.lastAppearance))
            return false;
        if (heading == null) {
            if (other.heading != null)
                return false;
        } else if (!heading.equals(other.heading))
            return false;
        return true;
    }
}

Issue.java:

package org.goobi.production.model.bibliography.course;

import java.util.HashSet;
import java.util.Set;

import org.joda.time.DateTimeConstants;
import org.joda.time.LocalDate;

/**
 * The class Issue represents the regular appearance of one (or the) issue of a
 * newspaper.
 * 
 * <p>
 * Newspapers, especially bigger ones, can have several issues that may differ
 * in time of publication (morning issue, evening issue, …), geographic
 * distribution (Edinburgh issue, London issue, …) and/or their days of
 * appearance (weekday issue: Mon—Fri, weekend issue: Sat, sports supplement:
 * Mon, property market: Wed, broadcasting programme: Thu). Furthermore there
 * may be exceptions in either that an issue didn’t appear on a date where, due
 * to the day of week, it usually does (i.e. on holidays) or an issue may have
 * appeared where, due to the day of week, it should not have.
 * </p>
 * 
 * <p>
 * Each issue can be modeled by one Issue object each, which are held by a Title
 * object which provides the dates of first and last appearance.
 * </p>
 */
public class Issue implements Cloneable {

    protected Set<LocalDate> additions;
    protected Set<Integer> daysOfWeek;
    protected Set<LocalDate> exclusions;
    protected String heading;

    public Issue() {
        heading = "";
        additions = new HashSet<LocalDate>();
        daysOfWeek = new HashSet<Integer>();
        exclusions = new HashSet<LocalDate>();
    }

    public Issue(String heading) {
        this();
        this.heading = heading;
    }

    public Issue(String heading, boolean monday, boolean tuesday, boolean wednesday, boolean thursday, boolean friday,
            boolean saturday, boolean sunday) {
        this(heading);
        if (monday)
            daysOfWeek.add(DateTimeConstants.MONDAY);
        if (tuesday)
            daysOfWeek.add(DateTimeConstants.TUESDAY);
        if (wednesday)
            daysOfWeek.add(DateTimeConstants.WEDNESDAY);
        if (thursday)
            daysOfWeek.add(DateTimeConstants.THURSDAY);
        if (friday)
            daysOfWeek.add(DateTimeConstants.FRIDAY);
        if (saturday)
            daysOfWeek.add(DateTimeConstants.SATURDAY);
        if (sunday)
            daysOfWeek.add(DateTimeConstants.SUNDAY);
    }

    public boolean addDayOfWeek(int dayOfWeek) {
        return daysOfWeek.add(dayOfWeek);
    }

    public Set<LocalDate> getAdditions() {
        return new HashSet<LocalDate>(additions);
    }

    public Set<Integer> getDaysOfWeek() {
        return new HashSet<Integer>(daysOfWeek);
    }

    public HashSet<LocalDate> getExclusions() {
        return new HashSet<LocalDate>(exclusions);
    }

    public boolean isMatch(LocalDate date) {
        return daysOfWeek.contains(date.getDayOfWeek()) && !exclusions.contains(date) || additions.contains(date);
    }

    public boolean removeDayOfWeek(int dayOfWeek) {
        return daysOfWeek.remove(dayOfWeek);
    }

    public void setAdditions(Set<LocalDate> additions) {
        this.additions = additions;
    }

    public void setDaysOfWeek(Set<Integer> daysOfWeek) {
        this.daysOfWeek = daysOfWeek;
    }

    public void setExclusions(Set<LocalDate> exclusions) {
        this.exclusions = exclusions;
    }

    @Override
    public String toString() {
        StringBuilder result = new StringBuilder();
        result.append(heading);
        result.append(" (");
        result.append(daysOfWeek.contains(DateTimeConstants.MONDAY) ? 'M' : '-');
        result.append(daysOfWeek.contains(DateTimeConstants.TUESDAY) ? 'T' : '-');
        result.append(daysOfWeek.contains(DateTimeConstants.WEDNESDAY) ? 'W' : '-');
        result.append(daysOfWeek.contains(DateTimeConstants.THURSDAY) ? 'T' : '-');
        result.append(daysOfWeek.contains(DateTimeConstants.FRIDAY) ? 'F' : '-');
        result.append(daysOfWeek.contains(DateTimeConstants.SATURDAY) ? 'S' : '-');
        result.append(daysOfWeek.contains(DateTimeConstants.SUNDAY) ? 'S' : '-');
        result.append(") +");
        result.append(additions.toString());
        result.append(" -");
        result.append(exclusions.toString());
        return result.toString();
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((additions == null) ? 0 : additions.hashCode());
        result = prime * result + ((exclusions == null) ? 0 : exclusions.hashCode());
        result = prime * result + ((heading == null) ? 0 : heading.hashCode());
        result = prime * result + ((daysOfWeek == null) ? 0 : daysOfWeek.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (!(obj instanceof Issue))
            return false;
        Issue other = (Issue) obj;
        if (additions == null) {
            if (other.additions != null)
                return false;
        } else if (!additions.equals(other.additions))
            return false;
        if (exclusions == null) {
            if (other.exclusions != null)
                return false;
        } else if (!exclusions.equals(other.exclusions))
            return false;
        if (heading == null) {
            if (other.heading != null)
                return false;
        } else if (!heading.equals(other.heading))
            return false;
        if (daysOfWeek == null) {
            if (other.daysOfWeek != null)
                return false;
        } else if (!daysOfWeek.equals(other.daysOfWeek))
            return false;
        return true;
    }
}
Was it helpful?

Solution

The code passes a reference of the same instance of Issue to both instances of one and two.

Title one = new Title(name, beginn, new LocalDate(1922, 6, 5));
Issue morningIssue = new Issue("Morning issue", true, true, true, true, true, true, false);
morningIssue.setExclusions(geschlossen);
one.addIssue(morningIssue);
one.optimise();

Title two = new Title(name, new LocalDate(1922, 6, 6), nameChange.minusDays(1));
two.addIssue(morningIssue);

Since both Title instances refer to the same Issue instance calling optimize() modifies the same instance of Issue that one and two have references to.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top